Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elementary Graph Algorithms

Similar presentations


Presentation on theme: "Elementary Graph Algorithms"— Presentation transcript:

1 Elementary Graph Algorithms
Graphs Elementary Graph Algorithms

2 Graphs A graph G = (V, E) V = set of vertices
E = set of edges = subset of V  V Thus |E| = O(|V|2)

3 Graph Variations Variations:
A connected graph has a path from every vertex to every other In an undirected graph: Edge (u,v) = edge (v,u) No self-loops In a directed graph: Edge (u,v) goes from vertex u to vertex v, notated uv

4 Graph Variations More variations:
A weighted graph associates weights with either the edges or the vertices E.g., a road map: edges might be weighted with distance A multigraph allows multiple edges between the same vertices E.g., the call graph in a program (a function can get called from multiple points in another function)

5 Representation of Graphs
Two standard ways. Adjacency Lists. Adjacency Matrix. a d c b a d c b 1 2 3 4 Comp 122, Fall 2004

6 Adjacency Lists Consists of an array Adj of |V| lists.
One list per vertex. For u  V, Adj[u] consists of all vertices adjacent to u. a b a b d c b c If weighted, store weights also in adjacency lists. c d c d d a d c b Comp 122, Fall 2004

7 Storage Requirement For directed graphs: out-degree(v) = |E|
Sum of lengths of all adj. lists is out-degree(v) = |E| vV Total storage: (V+E) For undirected graphs: degree(v) = 2|E| No. of edges leaving v No. of edges incident on v. Edge (u,v) is incident on vertices u and v. Comp 122, Fall 2004

8 Pros and Cons: adj list Pros Cons
Space-efficient, when a graph is sparse. Can be modified to support many graph variants. Cons Determining if an edge (u,v) G is not efficient. Have to search in u’s adjacency list. (degree(u)) time. (V) in the worst case. Comp 122, Fall 2004

9 Adjacency Matrix |V|  |V| matrix A.
Number vertices from 1 to |V| in some arbitrary manner. A is then given by: 1 2 a b c d 4 3 a d c b 1 2 3 4 A = AT for undirected graphs. Comp 122, Fall 2004

10 Space and Time Space: (V2).
Not memory efficient for large graphs. Time: to list all vertices adjacent to u: (V). Time: to determine if (u, v)  E: (1). Can store weights instead of bits for weighted graph. Comp 122, Fall 2004

11 Graph Searching Given: a graph G = (V, E), directed or undirected
Goal: methodically explore every vertex and every edge Ultimately: 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

12 Breadth-First Search “Explore” a graph, turning it into a tree
One vertex at a time Expand frontier of explored vertices across the breadth of the frontier Builds a tree over the graph Pick a source vertex to be the root Find (“discover”) its children, then their children, etc.

13 Breadth-First Search Will associate vertex “colors” to guide the algorithm 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

14 Breadth-First Search What does d[v] represent?
BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; What does d[v] represent? What does p[v] represent?

15 Breadth-First Search: Example
u v w x y

16 Breadth-First Search: Example
u v w x y Q: s

17 Breadth-First Search: Example
u 1 1 v w x y Q: w r

18 Breadth-First Search: Example
u 1 2 1 2 v w x y Q: r t x

19 Breadth-First Search: Example
u 1 2 2 1 2 v w x y Q: t x v

20 Breadth-First Search: Example
u 1 2 3 2 1 2 v w x y Q: x v u

21 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: v u y

22 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: u y

23 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: y

24 Breadth-First Search: Example
u 1 2 3 2 1 2 3 v w x y Q: Ø

25 BFS: The Code Again Touch every vertex: O(V)
BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list What will be the running time? Total running time: O(V+E)

26 BFS: The Code Again What will be the storage cost
BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; What will be the storage cost in addition to storing the tree? Total space used: O(max(degree(v))) = O(E)

27 Breadth-First Search: Properties
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 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

28 Depth-First Search Depth-first search is another strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

29 Depth-First Search Vertices initially colored white
Then colored gray when discovered Then black when finished

30 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time;

31 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What does d[u] represent?

32 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What does f[u] represent?

33 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; Will all vertices eventually be colored black?

34 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What will be the running time?

35 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times

36 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?

37 Depth-First Search: The Code
DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; So, running time of DFS = O(V+E)

38 Depth-First Sort Analysis
This running time argument is an informal example of amortized analysis “Charge” the exploration of edge to the edge: Each loop in DFS_Visit can be attributed to an edge in the graph Runs once/edge if directed graph, twice if undirected Thus loop will run in O(E) time, algorithm O(V+E) Considered linear for graph, b/c adj list requires O(V+E) storage Important to be comfortable with this kind of reasoning and analysis

39 DFS Example source vertex

40 DFS Example source vertex d f 1 | | | | | | | |

41 DFS Example source vertex d f 1 | | | 2 | | | | |

42 DFS Example source vertex d f 1 | | | 2 | | 3 | | |

43 DFS Example source vertex d f 1 | | | 2 | | 3 | 4 | |

44 DFS Example source vertex d f 1 | | | 2 | | 3 | 4 5 | |

45 DFS Example source vertex d f 1 | | | 2 | | 3 | 4 5 | 6 |

46 DFS Example source vertex d f 1 | 8 | | 2 | 7 | 3 | 4 5 | 6 |

47 DFS Example source vertex d f 1 | 8 | | 2 | 7 | 3 | 4 5 | 6 |

48 What is the structure of the grey vertices? What do they represent?
DFS Example source vertex d f 1 | 8 | | 2 | 7 9 | 3 | 4 5 | 6 | What is the structure of the grey vertices? What do they represent?

49 DFS Example source vertex d f 1 | 8 | | 2 | 7 9 |10 3 | 4 5 | 6 |

50 DFS Example source vertex d f 1 | 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

51 DFS Example source vertex d f 1 |12 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

52 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 |
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 |

53 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|

54 DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15
source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15

55 DFS Example d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15
source vertex d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15


Download ppt "Elementary Graph Algorithms"

Similar presentations


Ads by Google