CampusFlow
AlgorithmsGraph Algorithms

šŸ•øļø Graph Algorithms

Traverse, search, and optimize graph structures. Master BFS, DFS, shortest paths, and minimum spanning trees.

Overview

ā„¹ļø

What are Graph Algorithms?

Graphs model relationships between entities. Graph algorithms solve problems like finding paths, detecting cycles, computing connectivity, and optimizing flows. They're essential in social networks, navigation systems, and network routing.

Key Graph Algorithms

Breadth-First Search

Level-by-level graph traversal using a queue

Time: O(V + E)Space: O(V)

Depth-First Search

Explore as far as possible before backtracking

Time: O(V + E)Space: O(V)

Dijkstra's Algorithm

Shortest path from source to all vertices (positive weights)

Time: O((V+E) log V)Space: O(V)

Kruskal's Algorithm

Minimum spanning tree using union-find

Time: O(E log E)Space: O(V)

Topological Sort

Linear ordering of DAG vertices respecting edges

Time: O(V + E)Space: O(V)

Interactive Visualizer

Graph Visualization

šŸ•øļø

Breadth-First Search Graph Visualizer

Interact with nodes and edges to see how graph algorithms traverse and process graph structures.

Deep Dive: Breadth-First Search

Time Complexity

Time: O(V + E)

Space Complexity

Space: O(V)

Requires

Graph
šŸ’”

Algorithm Approach

BFS explores neighbors of the current vertex before moving to next level. It finds the shortest path in unweighted graphs and is ideal for level-order traversal.

python

from collections import deque

def bfs(graph, start):
    visited = set()
    queue = deque([start])
    visited.add(start)
    while queue:
        vertex = queue.popleft()
        print(vertex, end=" ")
        for neighbor in graph[vertex]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)
1

Represent Graph

Choose adjacency list, matrix, or edge list based on density and operations needed.

2

Initialize State

Set up visited set, distances, queue/stack/priority queue as needed.

3

Process Vertices

Follow algorithm-specific rules to explore neighbors and update state.

4

Return Results

Extract path, distances, ordering, or tree structure from computed state.

Real-World Applications

Social Networks

Friend recommendations, mutual connections, influence analysis

GPS Navigation

Shortest path computation for route planning and traffic optimization

Web Crawling

BFS crawls web pages level by level from a seed URL

Circuit Design

Topological sort for determining component order

Network Routing

OSPF protocol uses Dijkstra for shortest path routing

AI & Game Dev

Pathfinding (A*), game state exploration, decision trees

Interview Questions

What's the difference between BFS and DFS?

BFS uses a queue and finds shortest path in unweighted graphs. DFS uses a stack (or recursion) and uses less memory on average. BFS is better for finding closest solutions; DFS is better for exploring entire graphs.

Why doesn't Dijkstra work with negative edges?

Dijkstra assumes adding more edges always increases distance. With negative edges, a longer path might have lower total weight. Bellman-Ford handles negative edges by relaxing all edges V-1 times.

What are the tradeoffs between adjacency list and matrix?

Adjacency list: O(V+E) space, O(deg(v)) neighbor iteration. Adjacency matrix: O(V²) space, O(1) edge lookup. Lists are better for sparse graphs; matrices for dense graphs.

Knowledge Check

1. What data structure does BFS use?

2. What data structure does DFS use?

3. Can Dijkstra handle negative edge weights?

4. What does Kruskal's algorithm find?

5. When can topological sort be applied?

What's Next?

Advanced Graph Topics

Level up with these advanced concepts: