Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithms"— Presentation transcript:

1 Data Structures and Algorithms
Graph Algorithms II Gal A. Kaminka Computer Science Department

2 Representing a relation
Given a set S Given a mathematical relation R on S x S We can represent R as a directed graph as follows Gr = < V, E > V = S E = { <e1,e2> | e1,e2 in S, (e1,e2) in R }

3 Partial and total orders
R can represent a partial order, or total order Partial order: <a,b>, <c,b>, and unknown <a,c> nor <c,a> In graph: <a,b>, <c,b> and not <a,c>,<c,a> Sometimes called precedence relationship Total order: <a,b> <c,b> implies either <a,c> or <c,a> exists

4 Problem 1: Determining a Linearization of R
Let R be a relation with a partial order For example, order of dressing in the morning Underwear Socks Pants Shoes Shirt Belt Watch Tie Jacket

5 A Topological Sort A linearization of R:
An ordering of the vertices such that— If <u,v> is an edge, then u precedes v This is called a topological sort of R R may have multiple correct sorts

6 Topological Sorting Begin with DFS, and an empty list
Extend it such that: It works on unconnected graph Whenever it backtracks Put the current node in front of a list -- O(1) At end, list has vertices in a correct order Topological order (linearization)

7 Modified_DFS(graph g, vertex s, list l)
Top-Sort(graph g) l  new list Unmark all vertices in g for each vertex s if s unmarked, Modified_DFS(G, s, l) return l Modified_DFS(graph g, vertex s, list l) mark s for each edge <s, v> if v is not marked Modified_DFS(G, v, l) insert_in_front(l, s)

8 Example 2 3 Underwear Socks 2 2 Pants Shoes Shirt 4 2 1 Belt Watch Tie
Jacket Shirt Tie Socks Underwear Pants Shoes Belt Jacket Watch

9 Problem 2: All reachable vertices
Suppose we are given a graph G, and a vertex v We want: What vertices reachable from v? Reachable(G, v) l  new list Unmark all vertices in G Modified_DFS(G, v, l) return l

10 Problem 3: Computing transitive closure
The transitive closure of G: A graph G* is the transitive closure of G iff For any two edges <a,b>, <b,c> there exists <a,c> i.e., <a,b> in G*, if path exists from a to b in G. How can we find the transitive closure of G?

11 Method 1: Matrix-multiplication
Suppose we are given graph in matrix form A We can use matrix multiplication Redefine inner-product (dot-product) of vectors Normally [a b c].[x y z] = ax + by + cz We redefine: = max [min(a,x); min(b,y); min(c,z)] This is the boolean product All paths of length 2 can now be computed as: A x AT Paths of length d: Ad = A x A x …. A (d times)

12 Matrix multiplication method
Paths of length d: Ad = A x A x …. A (d times) To determine if a path exists of length d at most: Check A Check A2 Check A3 …. Check Ad We can calculate the path matrix of order d All paths of length d or less pathd = A || A2 || A3 || … || Ad

13 Calculating the closure
The transitive closure as path matrix (of order n) Pathn = matrix of paths of at most length n Closure(graph_matrix A) For k=1 to n For i=1 to n For j=1 to n p  path_n[i][] * A[][j] path_n[i][j]= p || path_n[i][j] return path_n

14 Calculating the closure
The transitive closure as path matrix (of order n) Pathn = matrix of paths of at most length n Closure(graph_matrix A) For k=1 to n For i=1 to n For j=1 to n p  pathn[i][] * A[][j] pathn[i][j]= p || pathn[i][j] return pathn Boolean dot product

15 Calculating the closure
The transitive closure as path matrix (of order n) Pathn = matrix of paths of at most length n Closure(graph_matrix A) For k=1 to n For i=1 to n For j=1 to n p  pathn[i][] * A[][j] pathn[i][j]= p || pathn[i][j] return pathn ith row jth column

16 Complexity of this method
Three nested loops, each 1 to n  N3 But inside innermost loop, dot-product  N So, overall, O(N4) While O(N4) is still tractable, it is not very good

17 Method 2: Improving naïve method
Observation: We are calculating A, AxA, AxAxA, … , An But really, we don’t care about intermediary steps We want An as quickly as possible. Idea: Use largest intermediate steps on itself Calculate path2 = path x path Then path4 = path2 x path2, path8 = path4 x path4,.. So then only O(log n) multiplications Complexity improved to O(n3 log n)

18 Method 3: Floyd-Warshall’s Algorithm
Observation: Suppose we restrict ourselves to first k vertices v0, v1, …. , vk Define pathk[i][j] = 1 iff There is a path from i to j that only uses first k vertices first k vertices  as intermediary vertices But, if there is a such a path p between i and j Then either: There is a path that uses only first k-1 vertices from i to j There is a path that uses only first k-1 vertices from i to k from k to j

19 Floyd-Warshall’s Algorithm
vi vj Case 0: There is no path from i to j using only vertices from the set { v0, …., vk } If so, then pathk[i][j] = 0

20 pathk[i][j] = 1 vi vj Case 1:
There is a path from i to j using only vertices from the set { v0, …., vk-1 } If so, then pathk-1[i][j] = 1

21 pathk[i][j] = 1 vi vj vk Case 2:
There is a path from i to j which uses vertex vk, and vertices from the set { v0, …., vk-1 } If so, then pathk-1[i][k] = 1 AND pathk-1[k][j] = 1

22 In other words…. Remember the transitive closure is matrix pathn
We can now recursively define pathn:

23 Warshall(graph_matrix G)
n|V|, for i1 to n for j1 to n if i=j OR <vi,vj> in E then path0[i][j]  1, else  0 for k1 to n return pathn

24 Floyd-Warshall’s Algorithm Notes
Run-time Complexity: O(n3) Can be modified to compute shortest paths between any two vertices Note that we use only 1/0 markings Can again use bits to save space (constant factor)


Download ppt "Data Structures and Algorithms"

Similar presentations


Ads by Google