1 / 40

Lecture 14: Graph Algorithms

Lecture 14: Graph Algorithms. Shang-Hua Teng. A. B. C. D. E. F. G. H. I. J. K. L. M. N. O. P. Undirected Graphs. A graph G = (V, E) V: vertices E : edges, unordered pairs of vertices from V  V (u,v) is same as (v,u) Thus |E| <= |V| (|V|-1)/2. A. B. C. D. E. F. G.

isha
Download Presentation

Lecture 14: Graph Algorithms

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. Lecture 14:Graph Algorithms Shang-Hua Teng

  2. A B C D E F G H I J K L M N O P Undirected Graphs • A graph G = (V, E) • V: vertices • E : edges, unordered pairs of vertices from V  V • (u,v) is same as (v,u) • Thus |E| <= |V| (|V|-1)/2

  3. A B C D E F G H I Undirected Graphs • A graph G = (V, E) • V: vertices • E : edges, ordered pairs of vertices from VV • (u,v) is different from (v,u) • Thus |E| <= |V| (|V|-1)

  4. Basic Graph Properties • An undirected graph is connected if it has a path from every vertex to every other • A directed graph is strongly connected if it has a path from every vertex to every other • A weighted graph associates weights with either the edges or the vertices • E.g., a road map: edges might be weighted w/ distance • A multigraph allows multiple edges between the same vertices

  5. Dense and Sparse Graphs • We will typically express running times in terms of |E| and |V| (often dropping the |’s) • If |E|  |V|2 the graph is dense • If |E|  |V| the graph is sparse • Different data structures may be used to express sparse and dense graph

  6. Graph in Applications • Internet: web graphs • Each page is a vertex • Each edge represent a hyperlink • Directed • GPS: highway maps • Each city is a vertex • Each freeway segment is an undirected edge • Undirected • Graphs are ubiquitous in computer science

  7. Representing Graphs • Assume V = {1, 2, …, n} • An adjacency matrixrepresents the graph as a n x n matrix A: • A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E

  8. Adjacency Matrix • Example: 1 a d 2 4 b c 3

  9. Adjacency Matrix Representation • Undirected graph  matrix A is symmetric • Storage requirements: O(V2) • A dense representation • The adjacency matrix is a dense representation • Usually too much storage for large graphs • But can be very efficient for small graphs • Most large interesting graphs are sparse • For example, trees and planar graphs (such as highway map) have |E| = O(|V|) • For this reason, we often needs more sparse representation

  10. Adjacency List Representation • Adjacency list: for each vertex v  V, store a list of vertices adjacent to v • Example: • Adj[1] = {2}{3} • Adj[2] = {3} • Adj[3] = {} • Adj[4] = {3} • Variation: can also keep a list of edges coming into vertex 1 2 4 3

  11. Adjacency List Representation • The degree of a vertex v in an undirected graph is equal to the number of incident edges • For directed graphs, we can define in-degree, out-degree for each vertex • For directed graphs, the total size of the adjacency lists is out-degree(v) = |E|takes (V + E) storage • For undirected graphs, the total size of the adjacent lists is  degree(v) = 2 |E| also (V + E) storage • So: Adjacency lists take O(V+E) storage

  12. Google Problem:Graph Searching • How to search and explore in the Web Space? • How to find all web-pages and all the hyperlinks? • Start from one vertex “www.google.com” • Systematically follow hyperlinks from the discovered vertex/web page • Build a search tree along the exploration

  13. General Graph Searching • Input: a graph G = (V, E), directed or undirected • Objective: methodically explore every vertex and every edge • Build a tree on the graph • Pick a vertex as the root • Choose certain edges to produce a tree • Note: might also build a forest if graph is not connected

  14. Breadth-first Search • Objective: Traverse all vertices of a graph G that can be reached from a starting vertex, called root • Method: • Search for all vertices that are directly reachable from the root (called level 1 vertices) • After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on. • In general level k vertices are directly reachable from a level k – 1 vertices

  15. An Example

  16. A B C D 0 E F G H I J K L M N O P

  17. A B C D 0 1 E F G H 1 1 I J K L M N O P

  18. A B C D 0 2 1 E F G H 1 1 I J K L 2 M N O P

  19. A B C D 0 3 2 1 E F G H 1 1 3 3 I J K L 2 M N O P 3 3

  20. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 M N O P 3 3

  21. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5

  22. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5

  23. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5

  24. Breadth-First Search BFS(G, s) • For each vertex u in V – {s}, • color[u] = white; d[u] = infty; p[u] = NIL • color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = {} • ENQUEUE(Q,s) // Q is a FIFO queue • while (Q not empty) • u = DEQUEUE(Q) • for each v  Adj[u] • if color[v] = WHITE • then color[v] = GREY • d[v] = d[u] + 1; p[v] = u • ENQUEUE(Q, v); • color[u] = BLACK;

  25. Breadth-first Search Algorithm • Algorithm BFS(s): • initialize container L to contain start vertex s. • i 0 • while Li is not empty do • create container Li+1 to initially be empty • for each vertex v in Li do • for each edge e incident on v do • if edge e is unexplored then • let w be the other endpoint of e • if vertex w is unexplored then • label e as a discovery edge • insert w into Li+1 • else • label e as a cross edge • i  i + 1

  26. Breadth-first Search • Visited all vertices reachable from the root • A spanning tree • For any vertex at level i, the spanning tree path from s to i has i edges, and any other path from s to i has at leasti edges (shortest path property) • Edges not in the spanning tree are at most, 1 level apart (level by level property)

  27. Breadth-First Search: the Color Scheme • White vertices have not been discovered • All vertices start out white • Grey vertices are discovered but not fully explored • They may be adjacent to white vertices • Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices • Explore vertices by scanning adjacency list of grey vertices

  28. Example r s t u         v w x y

  29. Example r s t u  0       v w x y Q: s

  30. Example r s t u 1 0    1   v w x y Q: w r

  31. Example r s t u 1 0 2   1 2  v w x y Q: r t x

  32. Example r s t u 1 0 2  2 1 2  v w x y Q: t x v

  33. Example r s t u 1 0 2 3 2 1 2  v w x y Q: x v u

  34. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y

  35. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y

  36. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y

  37. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø

  38. Properties of BFS • BFS calculates the shortest-path distance to the source node • Shortest-path distance (s,v) = minimum number of edges from s to v, or  if v not reachable from s • Prove using blackboard • BFS builds breadth-first tree, in which paths to root represent shortest paths in G • Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

  39. Properties of BFS • Proposition: Let G be an undirected graph on which a BFS traversal starting at vertex s has been performed. Then • The traversal visits all vertices in the connected component of s . • The discovery-edges form a spanning tree T, which we call the BFS tree, of the connected component of s • For each vertex v at level i, the path of the BFS tree T between s and v has i edges, and any other path of G between s and v has at least i edges. • If (u, v) is an edge that is not in the BFS tree, then the level numbers of u and v differ by at most one.

  40. Properties of BFS • Proposition: Let G be a graph with n vertices and m edges. A BFS traversal of G takes time O(n + m). Also, there exist O(n + m) time algorithms based on BFS for the following problems: • Testing whether G is connected. • Computing a spanning tree of G • Computing the connected components of G • Computing, for every vertex v of G, the minimum number of edges of any path between s and v.

More Related