1 / 13

Graph Algorithms: What is a Graph, Traversals, and Applications

Learn about graphs, their properties, and how to perform breadth-first search (BFS) and depth-first search (DFS) traversals. Discover applications of DFS such as topological sort and finding strongly connected components.

Download Presentation

Graph Algorithms: What is a Graph, Traversals, and Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • 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)

  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) What if the graph contains a cycle?

  10. 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.

  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. How to find strongly connected components ?

  12. 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)

  13. Many other applications of D/BFS • DFS • find articulation points • find bridges • BFS • e.g. sokoban

More Related