Download presentation
Presentation is loading. Please wait.
Published byIris Wilcox Modified over 6 years ago
1
Graph Algorithms What is a graph? V - vertices E µ V x V - edges
directed / undirected Representation: adjacency matrix adjacency lists Why graphs?
2
Graph Algorithms Graph properties: connected cyclic …
Tree – a connected acyclic (undirected) graph
3
Graph Traversals Objective: list all vertices reachable from a given vertex s
4
Breadth-first search (BFS)
Finds all vertices “reachable” from a starting vertex. Byproduct: computes distances from the starting vertex to every vertex BFS ( G=(V,E), s ) seen[v]=false, dist[v]=1 for every vertex v beg=1; end=2; Q[1]=s; seen[s]=true; dist[s]=0; while (beg<end) do head=Q[beg]; for every u s.t. (head,u) is an edge and not seen[u] do Q[end]=u; dist[u]=dist[head]+1; seen[u]=true; end++; beg++;
5
Depth-first search (DFS)
Finds all vertices “reachable” from a starting vertex, in a different order than BFS. DFS-RUN ( G=(V,E), s ) seen[v]=false for every vertex v DFS(s) DFS(v) seen[v]=true for every neighbor u of v if not seen[u] then DFS(u)
6
Applications of DFS: topological sort
Def: A topological sort of a directed graph is an order of vertices such that every edge goes from “left to right.”
7
Applications of DFS: topological sort
Def: A topological sort of a directed graph is an order of vertices such that every edge goes from “left to right.”
8
Applications of DFS: topological sort
Def: A topological sort of a directed graph is an order of vertices such that every edge goes from “left to right.”
9
Applications of DFS: topological sort
TopSort ( G=(V,E) ) for every vertex v seen[v]=false fin[v]=1 time=0 for every vertex s if not seen[s] then DFS(s) DFS(v) seen[v]=true for every neighbor u of v if not seen[u] then DFS(u) time++ fin[v]=time (and output v)
10
Applications of DFS: topological sort
TopSort ( G=(V,E) ) for every vertex v seen[v]=false fin[v]=1 time=0 for every vertex s if not seen[s] then DFS(s) DFS(v) seen[v]=true for every neighbor u of v if not seen[u] then DFS(u) time++ fin[v]=time (and output v) What if the graph contains a cycle?
11
Appl.DFS: strongly connected components
Vertices u,v are in the same strongly connected component if there is a (directed) path from u to v and from v to u.
12
Appl.DFS: strongly connected components
Vertices u,v are in the same strongly connected component if there is a (directed) path from u to v and from v to u. How to find strongly connected components ?
13
Appl.DFS: strongly connected components
STRONGLY-CONNECTED COMPONENTS ( G=(V,E) ) for every vertex v seen[v]=false fin[v]=1 time=0 for every vertex s if not seen[s] then DFS(G,s) (the finished-time version) compute G^T by reversing all arcs of G sort vertices by decreasing finished time seen[v]=false for every vertex v for every vertex v do if not seen[v] then output vertices seen by DFS(v)
14
Many other applications of D/BFS
DFS find articulation points find bridges BFS e.g. sokoban
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.