Tags

Undirected Graphs

Undirected Graphs

1 lesson
5 problems

undirected-graphs

Data Structures

1 lesson

Graphs: Representation Basics

Free
Beginner

55 min

2 prereqs

A social network is a graph, a road map is a graph, an `import` graph in your codebase is a graph, and so is the dependency tree of a package manager. The reason graphs feel everywhere is that they are the most general relationship structure: just vertices and edges, with no required root, no acyclic constraint, and no fixed branching factor. **Graphs: Representation Basics** is where you stop drawing them on whiteboards and start storing them in code. This lesson defines the core vocabulary (vertex, edge, degree, path, cycle, directed and undirected, connected and disconnected) and walks through the three canonical representations: an adjacency list backed by a hash map of vertex to neighbors, an adjacency matrix for dense graphs and `O(1)` edge-existence queries, and an edge list for algorithms that just iterate over edges. You will analyze the space and time trade-offs of each so you can pick the right one before writing any traversal logic. In **Hash Map (Dictionary) Basics**, you saw that a hash map maps keys to values in expected `O(1)`; an adjacency list is exactly that, mapping each vertex to its list of neighbors. **Queue & Deque** gave you the FIFO machinery that BFS will run on top of these representations once traversal lessons begin. With a graph stored in memory, the algorithms track is where it comes alive: BFS, DFS, shortest paths, connected components, and the rest of the graph algorithm canon.

Not Started

0%

Graphs
Graph Representation
Adjacency List
Adjacency Matrix
Directed Graphs
Undirected Graphs
Data Structures
Beginner
Free

Practice Problems

5 problems

Number of Connected Components

Not Started
Medium

Given n nodes and a list of undirected edges, find the number of connected components in the graph.

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1k

24

Graph Valid Tree

Not Started
Medium

Given n nodes and a list of undirected edges, determine if these edges form a valid tree (connected and acyclic).

Graphs
DFS
Union-Find / DSU
Cycle Detection
Undirected Graphs
Intermediate

932

16

Minimum Height Trees

Not Started
Medium

Given a tree of n nodes, find all roots that would minimize the tree's height when the tree is rooted at them.

Graphs
BFS
Topological Sort
Undirected Graphs
Intermediate

903

8

Number of Provinces

Free
Not Started
Medium

Given an adjacency matrix representing connections between cities, find the total number of provinces (connected components).

Graphs
DFS
Union-Find / DSU
Connected Components
Undirected Graphs
Intermediate

1.1k

18

Redundant Connection

Not Started
Medium

Given a graph that started as a tree with one extra edge added, find and return the edge that can be removed to make it a tree again.

Graphs
Union-Find / DSU
Cycle Detection
Undirected Graphs
Intermediate

348

6