Download presentation
Presentation is loading. Please wait.
Published byLinda McKinney Modified over 9 years ago
1
D ESIGN & A NALYSIS OF A LGORITHM 04 – G RAPH A LGORITHMS Informatics Department Parahyangan Catholic University
2
M OTIVATION We have studied how to model and simplify real life problems into graphs In this course topic, we will further discuss how to represent a graph in a computer program and implements some fundamental graph algorithms Adjacency Matrix representation Adjacency List representation DFS & BFS Traversal Dijkstra’s shortest path (later) Minimum Spanning Tree (later) In term of programming, a vertex usually carries extra information, thus it is sometimes called a node
3
G RAPH R EPRESENTATION A DJACENCY M ATRIX An adjacency matrix representation of a graph is a n-by-n matrix of Boolean values, with the entry in row v and column w defined to be 1 if there is an edge connecting vertex v and vertex w in the graph, and 0 otherwise. Example: 1 3 2 4 6 5 123456 1 010000 2 101101 3 010100 4 011010 5 000101 6 010010
4
When the graph is directed, row v column w is defined to be 1 if there is an edge from v to w Example: G RAPH R EPRESENTATION A DJACENCY M ATRIX 1 3 2 4 6 5 123456 1 010000 2 000001 3 010100 4 010010 5 000001 6 000000
5
If we need to label the edges, we can use integer values instead of Booleans Example: relationship graph 123456 1 010000 2 102407 3 020300 4 043050 5 000506 6 070060 (1)Ann (3)Tom (2)Bob (4)Lea (6)Noah (5)Jim (6)close friend (1)spouse (4)co-worker (3)lover (2)buddy (5)family (7)acquaintance 1 Ann 2 Bob 3 Tom 4 Lea 5 Jim 6 Noah 1 spouse 2 buddy 3 lover 4 co-worker 5 family 6 close friend 7 acquaintance verticesedges matrix
6
G RAPH R EPRESENTATION A DJACENCY L IST Adjacency matrix representation is easy to implement with arrays, however, when the graph is sparse (has few edges), it wastes a lot of memory space. Adjacency list tries to answer this problem. Edges’ information are stored in an array of linked list.
7
G RAPH R EPRESENTATION A DJACENCY L IST :: E XAMPLE 1 3 2 4 6 5 1 2 3 4 5 6 23142432546256 1 3 2 4 6 5 1 2 3 4 5 6 2624256
8
We can add vertex’s and edge’s information to the linked list’s node (1)Ann (3)Tom (2)Bob (4)Lea (6)Noah (5)Jim (6)close friend (1)spouse (4)co-worker (3)lover (2)buddy (5)family (7)acquaintance G RAPH R EPRESENTATION A DJACENCY L IST :: E XAMPLE 1 Ann 2 Bob 3 Tom 4 Lea 5 Jim 6 Noah 2spouse6 acquaintance 2buddy4lover5family2 co-worker6close-friend
9
DFS A LGORITHM We have discussed DFS traversal in a tree or graph. This traversal method can be easily implemented recursively: To visit a vertex, we mark it as having been visited, then (recursively) visit all the vertices that are adjacent to it and that have not yet been marked To mark which vertex has been visited, we can use a Boolean array, initially set to 0
10
DFS A LGORITHM DFS(G) initialize isVisited[0..n] to false for each unvisited vertex v of G DFS_RECURSIVE(v) DFS_RECURSIVE(x) isVisited[x] = true process(x) for each unvisited vertex v adjacent to x DFS_RECURSIVE(v) DFS(G) initialize isVisited[0..n] to false for each unvisited vertex v of G DFS_RECURSIVE(v) DFS_RECURSIVE(x) isVisited[x] = true process(x) for each unvisited vertex v adjacent to x DFS_RECURSIVE(v) Simple DFS Algorithm
11
DFS A LGORITHM Sometimes we need to know the order of vertex visited Use an array of n integer to store the order of vertex visited, instead of array of Boolean isVisited. The array is initially set to 0 (unvisited). Sometimes we also need to store the traversal tree Use an array of n integer to store the parent’s index of each vertex (remember, each vertex of a tree has exactly one parent, except the root). The array is initially set to 0. The vertex (or vertices) which parent remains 0 is the root of the traversal tree (or forest if the graph is disconnected).
12
DFS A LGORITHM // global variable ctr = 1 DFS(G) initialize ord[0..n] to 0 initialize parent[0..n] to 0 for each unvisited vertex v of G DFS_RECURSIVE(v) DFS_RECURSIVE(x) ord[x] = ctr ctr = ctr+1 process(x) for each unvisited vertex v adjacent to x parent[v] = x DFS_RECURSIVE(v) // global variable ctr = 1 DFS(G) initialize ord[0..n] to 0 initialize parent[0..n] to 0 for each unvisited vertex v of G DFS_RECURSIVE(v) DFS_RECURSIVE(x) ord[x] = ctr ctr = ctr+1 process(x) for each unvisited vertex v adjacent to x parent[v] = x DFS_RECURSIVE(v) DFS Algorithm with ord and parent array
13
DFS T REE D E B F G C I A H D E B F G C I A H D EB F G C I A H DFS Tree Recall…
14
DFS T REE P ROPERTIES During DFS traversal on vertex v, we know a vertex u adjacent to v is already visited iff ord[u] is not 0. In fact, we can further examine : If ord[u] < ord[v], then v is u ’s ancestor, because v is visited before u If ord[u] > ord[v], then v is u ’s descendant, because v is visited after u If parent[u] is v, then v is u ’s ancestor and also a direct parent of u Based on these observation, we can “upgrade” our DFS Tree to show more information
15
DFS T REE D E B F G C I A H D E B F G C I A H D E B F G C I A H DFS Tree 1 2 3 4 5 6 A 7 8 9 B A H F E F I B C G G BF = parent = down edge DOWN = back edge BACK
16
DFS T REE D E B F G C I A H D E B F G C I A H ABCDEFGHI parent 0FBEFICAH ord 178654923 D E B F G C I A H DFS Tree A 1 2 3 4 5 6 7 8 9 B A H F E F I B C G G BF #back edge = #down edge = #chord = #edges that form circuits #back edge = #down edge = #chord = #edges that form circuits
17
E XERCISE Draw the DFS traversal tree and fill the ord & parent array for this graph ! D E B F G C I A H J K
18
DFS A LGORITHM Simple to implement using recursive The base of many useful algorithm, such as Counting component(s) in a graph Connectivity Flood-fill Topological sort Finding bridge Finding articulation point Etc.
19
DFS A LGORITHM C OUNTING COMPONENT ( S ) CountComponent(G) component = 0 for each unvisited vertex v of G component = component + 1 DFS_RECURSIVE(v) return component CountComponent(G) component = 0 for each unvisited vertex v of G component = component + 1 DFS_RECURSIVE(v) return component Same as counting how many trees in DFS forest
20
DFS A LGORITHM C ONNECTIVITY isConnected(G) component = 0 for each unvisited vertex v of G component = component + 1 DFS_RECURSIVE(v) return (component == 1) isConnected(G) component = 0 for each unvisited vertex v of G component = component + 1 DFS_RECURSIVE(v) return (component == 1) Returns true if graph G is connected, returns false otherwise If #component > 1, then graph must be disconnected
21
“Flooding” a component that contains vertex v Same as performing DFS on a single tree only Sometimes we’re interested to know which vertex (or vertices) are flooded, in such case, we can return the ord array DFS A LGORITHM F LOOD F ILL FloodFill(v) DFS_RECURSIVE(v) return ord FloodFill(v) DFS_RECURSIVE(v) return ord
22
P ROBLEM SCHEDULING TASKS IN THE MORNING Wear socks Wear shoes Take a bath Wear clothes Eat breakfast Cook breakfast Get inside a car Drive to campus Get schoolbag Brew a cup of coffee Drink coffee
23
T OPOLOGICAL S ORTING Topological sort of a directed graph is a linear ordering of its vertices such that for every directed edge ( u v ), vertex u comes before vertex v in the ordering Only possible iff the graph does not have any directed cycle D E C D E C OK Not OK
24
P ROBLEM SCHEDULING TASKS IN THE MORNING Wear socks Wear shoes Take a bath Wear clothes Eat breakfast Cook breakfast Get inside a car Drive to campus Get schoolbag Brew a cup of coffee Drink coffee in=1 in=2out=3 out=8 in=4out=7 Eat breakfast Drive to campus Drink coffee Cook breakfast in=9out=10 in=5out=6 Take a bath in=15out=20 Wear socks in=11out=14 Wear clothes in=16out=19 Get inside a car in=17out=18 Wear shoes in=12out=13 Get schoolbag in=21out=22 Descending sort by “out” : Get schoolbag(22) – Take a bath(20) – Wear clothes(19) – Get inside a car(18) – Wear socks (14) – Wear shoes (13) – Cook Breakfast(10) – Brew a cup of coffee(8) – Eat Breakfast(7) – Drive to campus(6) – Drink coffee(3)
25
Descending sort by “out” : Get schoolbag(22) – Take a bath(20) – Wear clothes(19) – Get inside a car(18) – Wear socks (14) – Wear shoes (13) – Cook Breakfast(10) – Brew a cup of coffee(8) – Eat Breakfast(7) – Drive to campus(6) – Drink coffee(3) P ROBLEM SCHEDULING TASKS IN THE MORNING Wear socks Wear shoes Take a bath Wear clothes Eat breakfast Cook breakfast Get inside a car Drive to campus Get schoolbag Brew a cup of coffee Drink coffee
26
T OPOLOGICAL S ORTING A LGORITHM // global variable ctr = 1 // global stack S = empty stack DFS(G) initialize ord_in[0..n] to 0 initialize ord_out[0..n] to 0 for each unvisited vertex v of G DFS_RECURSIVE(v) return S DFS_RECURSIVE(x) ord_in[x] = ctr ctr = ctr+1 process(x) for each vertex v adjacent to x if ord_in[v] != 0 AND ord_out[v] == 0 stop, graph is not DAG else if ord_in[v] == 0//unvisited DFS_RECURSIVE(v) S.push(x) ord_out[x] = ctr ctr = ctr + 1 // global variable ctr = 1 // global stack S = empty stack DFS(G) initialize ord_in[0..n] to 0 initialize ord_out[0..n] to 0 for each unvisited vertex v of G DFS_RECURSIVE(v) return S DFS_RECURSIVE(x) ord_in[x] = ctr ctr = ctr+1 process(x) for each vertex v adjacent to x if ord_in[v] != 0 AND ord_out[v] == 0 stop, graph is not DAG else if ord_in[v] == 0//unvisited DFS_RECURSIVE(v) S.push(x) ord_out[x] = ctr ctr = ctr + 1
27
E XERCISE Find the topological order for these vertices ! D E B F G C I A H J K
28
BFS A LGORITHM Suppose that we want to find a shortest path between two specific vertices in a graph (a path connecting the vertices with the property that no other path connecting those vertices has fewer edges) The classical method for accomplishing this task is breadth-first search Can be easily implemented with a queue
29
BFS T REE P ROPERTY Recall… D E B F G C I A H D E B F G C I A H D E B F GC I A H BFS Tree For any vertex v in the BFS tree rooted at r, the tree path from r to v corresponds to a shortest path from r to v in the corresponding graph
30
BFS A LGORITHM BFS(G) Q = empty queue initialize isVisited[0..n] to false for each unvisited vertex v of G Q.enqueue(v) isVisited[v] = true while(Q is not empty) x = Q.dequeue() process(x) for each unvisited vertex u adjacent to x isVisited[v] = true Q.enqueue(u) BFS(G) Q = empty queue initialize isVisited[0..n] to false for each unvisited vertex v of G Q.enqueue(v) isVisited[v] = true while(Q is not empty) x = Q.dequeue() process(x) for each unvisited vertex u adjacent to x isVisited[v] = true Q.enqueue(u) Simple BFS Algorithm
31
BFS A LGORITHM To find the shortest path from any node to the root, we need to store the BFS traversal tree Adds array parent to store the parent of each node Similar to DFS algorithm, we might want to know the order the vertices visited Use array ord to store the order each vertex visited
32
BFS A LGORITHM BFS(G) ctr = 1 Q = empty queue initialize ord[0..n] to 0 initialize parent[0..n] to 0 for each unvisited vertex v of G Q.enqueue(v) ord[v] = ctr ctr = ctr+1 while(Q is not empty) x = Q.dequeue() process(x) for each unvisited vertex u adjacent to x parent[u] = x ord[x] = ctr ctr = ctr+1 Q.enqueue(u) BFS(G) ctr = 1 Q = empty queue initialize ord[0..n] to 0 initialize parent[0..n] to 0 for each unvisited vertex v of G Q.enqueue(v) ord[v] = ctr ctr = ctr+1 while(Q is not empty) x = Q.dequeue() process(x) for each unvisited vertex u adjacent to x parent[u] = x ord[x] = ctr ctr = ctr+1 Q.enqueue(u) BFS Algorithm with parent and ord arrays
33
BFS A LGORITHM Same as DFS, BFS algorithm also able to solve basic connectivity problems, such as: flood fill, counting component, spanning tree. Note: topological sort is not a connectivity problem The solutions of these problems only depends of the ability to examine every vertex of the graph, not the order we visit the vertices
34
DFS V. S. BFS D E B F G C I A H D EB F G C I A H DFS Tree D E B F GC I A H BFS Tree Consider our previous example DFS tree has several interesting properties such as down/back edge, which is useful to solve some important problems (i.e., finding bridge and articulation point) However, DFS tree is usually deep. Computer program usually has a limitation on how much recursive calls possible, so for large graph, recursive DFS might not feasible. In such case, we use BFS (or iterative version of DFS)
35
DFS V. S. BFS EXAMPLE Tree height = #vertices = n 2 Tree height = 2n-1
36
T IME C OMPLEXITY Graph traversal is meant to visit each vertex exactly once, thus there might be some edges remain unvisited seems like O(#vertices) In practice we still need to visit every edges (including the chords) to check whether the vertex on its other end is already visited So the time complexity for traversing a graph with V vertices and E edges is O(V+E) for adjacency list representation O(V 2 ) for adjacency matrix representation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.