Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.

Similar presentations


Presentation on theme: "David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms."— Presentation transcript:

1 David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms

2 David Luebke 2 1/6/2016 Review: Graphs l A graph G = (V, E) n V = set of vertices, E = set of edges n Dense graph: |E|  |V| 2 ; Sparse graph: |E|  |V| n Undirected graph: u Edge (u,v) = edge (v,u) u No self-loops n Directed graph: u Edge (u,v) goes from vertex u to vertex v, notated u  v n A weighted graph associates weights with either the edges or the vertices

3 David Luebke 3 1/6/2016 Review: Representing Graphs l Assume V = {1, 2, …, n} l An adjacency matrix represents the graph as a n x n matrix A: n A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E n Storage requirements: O(V 2 ) u A dense representation n But, can be very efficient for small graphs u Especially if store just one bit/edge u Undirected graph: only need one diagonal of matrix

4 David Luebke 4 1/6/2016 Review: Adjacency Matrix l Example: 1 24 3 a d bc A1234 10110 20010 30000 40010

5 David Luebke 5 1/6/2016 Review: Adjacency List l Adjacency list: for each vertex v  V, store a list of vertices adjacent to v l Example: n Adj[1] = {2,3} n Adj[2] = {3} n Adj[3] = {} n Adj[4] = {3} l Storage: O(V+E) n Good for large, sparse graphs (e.g., planar maps) 1 24 3

6 David Luebke 6 1/6/2016 Graph Searching l Given: a graph G = (V, E), directed or undirected l Goal: methodically explore every vertex and every edge l Ultimately: build a tree on the graph n Pick a vertex as the root n Choose certain edges to produce a tree n Note: might also build a forest if graph is not connected

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

8 David Luebke 8 1/6/2016 Breadth-First Search l Again will associate vertex “colors” to guide the algorithm n White vertices have not been discovered u All vertices start out white n Grey vertices are discovered but not fully explored u They may be adjacent to white vertices n Black vertices are discovered and fully explored u They are adjacent only to black and gray vertices l Explore vertices by scanning adjacency list of grey vertices

9 David Luebke 9 1/6/2016 Breadth-First Search BFS(G, s) { initialize vertices; Q = {s};// Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What does v->p represent? What does v->d represent?

10 David Luebke 10 1/6/2016 Breadth-First Search: Example         rstu vwxy

11 David Luebke 11 1/6/2016 Breadth-First Search: Example   0      rstu vwxy s Q:

12 David Luebke 12 1/6/2016 Breadth-First Search: Example 1  0 1     rstu vwxy w Q: r

13 David Luebke 13 1/6/2016 Breadth-First Search: Example 1  0 1 2 2   rstu vwxy r Q: tx

14 David Luebke 14 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2   rstu vwxy Q: txv

15 David Luebke 15 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2 3  rstu vwxy Q: xvu

16 David Luebke 16 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: vuy

17 David Luebke 17 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: uy

18 David Luebke 18 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: y

19 David Luebke 19 1/6/2016 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: Ø

20 David Luebke 20 1/6/2016 BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What will be the running time? 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 Total running time: O(V+E)

21 David Luebke 21 1/6/2016 BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What will be the storage cost in addition to storing the tree? Total space used: O(max(degree(v))) = O(E)

22 David Luebke 22 1/6/2016 Breadth-First Search: Properties l BFS calculates the shortest-path distance to the source node n Shortest-path distance  (s,v) = minimum number of edges from s to v, or  if v not reachable from s n Proof given in the book (p. 472-5) l BFS builds breadth-first tree, in which paths to root represent shortest paths in G n Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

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

24 David Luebke 24 1/6/2016 Depth-First Search l Vertices initially colored white l Then colored gray when discovered l Then black when finished

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

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

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

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

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

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

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

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

33 David Luebke 33 1/6/2016 Depth-First Sort Analysis l This running time argument is an informal example of amortized analysis n “Charge” the exploration of edge to the edge: u Each loop in DFS_Visit can be attributed to an edge in the graph u Runs once/edge if directed graph, twice if undirected u 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 n Important to be comfortable with this kind of reasoning and analysis

34 David Luebke 34 1/6/2016 DFS Example source vertex

35 David Luebke 35 1/6/2016 DFS Example 1 | | | | | | | | source vertex d f

36 David Luebke 36 1/6/2016 DFS Example 1 | | | | | | 2 | | source vertex d f

37 David Luebke 37 1/6/2016 DFS Example 1 | | | | |3 | 2 | | source vertex d f

38 David Luebke 38 1/6/2016 DFS Example 1 | | | | |3 | 4 2 | | source vertex d f

39 David Luebke 39 1/6/2016 DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f

40 David Luebke 40 1/6/2016 DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f

41 David Luebke 41 1/6/2016 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

42 David Luebke 42 1/6/2016 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

43 David Luebke 43 1/6/2016 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the grey vertices? What do they represent?

44 David Luebke 44 1/6/2016 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

45 David Luebke 45 1/6/2016 DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

46 David Luebke 46 1/6/2016 DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

47 David Luebke 47 1/6/2016 DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

48 David Luebke 48 1/6/2016 DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

49 David Luebke 49 1/6/2016 DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

50 David Luebke 50 1/6/2016 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

51 David Luebke 51 1/6/2016 DFS: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex u The tree edges form a spanning forest u Can tree edges form cycles? Why or why not?

52 David Luebke 52 1/6/2016 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edges

53 David Luebke 53 1/6/2016 DFS: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor u Encounter a grey vertex (grey to grey)

54 David Luebke 54 1/6/2016 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edges

55 David Luebke 55 1/6/2016 DFS: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent u Not a tree edge, though u From grey node to black node

56 David Luebke 56 1/6/2016 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edges

57 David Luebke 57 1/6/2016 DFS: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent n Cross edge: between a tree or subtrees u From a grey node to a black node

58 David Luebke 58 1/6/2016 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges

59 David Luebke 59 1/6/2016 DFS: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent n Cross edge: between a tree or subtrees l Note: tree & back edges are important; most algorithms don’t distinguish forward & cross

60 David Luebke 60 1/6/2016 DFS: Kinds Of Edges l Thm 23.9: If G is undirected, a DFS produces only tree and back edges l Proof by contradiction: n Assume there’s a forward edge u But F? edge must actually be a back edge (why?) source F?

61 David Luebke 61 1/6/2016 DFS: Kinds Of Edges l Thm 23.9: If G is undirected, a DFS produces only tree and back edges l Proof by contradiction: n Assume there’s a cross edge u But C? edge cannot be cross: u must be explored from one of the vertices it connects, becoming a tree vertex, before other vertex is explored u So in fact the picture is wrong…both lower tree edges cannot in fact be tree edges source C?

62 David Luebke 62 1/6/2016 DFS And Graph Cycles l Thm: An undirected graph is acyclic iff a DFS yields no back edges n If acyclic, no back edges (because a back edge implies a cycle n If no back edges, acyclic u No back edges implies only tree edges (Why?) u Only tree edges implies we have a tree or a forest u Which by definition is acyclic l Thus, can run DFS to find whether a graph has a cycle

63 David Luebke 63 1/6/2016 DFS And Cycles l How would you modify the code to detect cycles? DFS(G) { for each vertex u  G->V { u->color = WHITE; } time = 0; for each vertex u  G->V { if (u->color == WHITE) DFS_Visit(u); } DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] { if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; time = time+1; u->f = time; }

64 David Luebke 64 1/6/2016 DFS And Cycles l What will be the running time? DFS(G) { for each vertex u  G->V { u->color = WHITE; } time = 0; for each vertex u  G->V { if (u->color == WHITE) DFS_Visit(u); } DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] { if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; time = time+1; u->f = time; }

65 David Luebke 65 1/6/2016 DFS And Cycles l What will be the running time? l A: O(V+E) l We can actually determine if cycles exist in O(V) time: n In an undirected acyclic forest, |E|  |V| - 1 n So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way

66 David Luebke 66 1/6/2016 The End

67 David Luebke 67 1/6/2016 Exercise 1 Feedback l First, my apologies… n Harder than I thought n Too late to help with midterm l Proof by substitution: n T(n) = T(n/2 +  n) + n n Most people assumed it was O(n lg n)…why? u Resembled proof from class: T(n) = 2T(n/2 + 17) + n u The correct intuition: n/2 dominates  n term, so it resembles T(n) = T(n/2) + n, which is O(n) by m.t. n Still, if it’s O(n) it’s O(n lg n), right?

68 David Luebke 68 1/6/2016 Exercise 1: Feedback l So, prove by substitution that T(n) = T(n/2 +  n) + n = O(n lg n) l Assume T(n)  cn lg n l Then T(n)  c(n/2 +  n) lg (n/2 +  n)  c(n/2 +  n) lg (n/2 + n)  c(n/2 + n) lg (3n/2)  …


Download ppt "David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms."

Similar presentations


Ads by Google