Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 223 times |
Download: | 0 times |
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
Pants
Belt
Socks
ShoesShirt
Tie
Jacket
Watch
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
Top-Sort(graph g)
1. mark s
2. for each edge <s, v>
3. if v is not marked
4. Modified_DFS(G, v, l)
5. insert_in_front(l, s)
1. l new list
2. Unmark all vertices in g
3. for each vertex s
4. if s unmarked, Modified_DFS(G, s, l)
5. return l
Modified_DFS(graph g, vertex s, list l)
8
Example
Underwear
Pants
Belt
Socks
ShoesShirt
Tie
Jacket
Watch
Underwear
1
2 3
4
Pants Shoes Belt JacketSocksTie
Shirt
Watch
2
2
2
2
4
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)
1. l new list
2. Unmark all vertices in G
3. Modified_DFS(G, v, l)
4. 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)
1. For k=1 to n
2. For i=1 to n
3. For j=1 to n
4. p path_n[i][] * A[][j]
5. path_n[i][j]= p || path_n[i][j]
6. 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)
1. For k=1 to n
2. For i=1 to n
3. For j=1 to n
4. p pathn[i][] * A[][j]
5. pathn[i][j]= p || pathn[i][j]
6. 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)
1. For k=1 to n
2. For i=1 to n
3. For j=1 to n
4. p pathn[i][] * A[][j]
5. pathn[i][j]= p || pathn[i][j]
6. return pathn
jth column
ith row
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
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
vi vj
20
pathk[i][j] = 1
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
vi vj
21
pathk[i][j] = 1
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
vi vjvk
22
In other words….
Remember the transitive closure is matrix pathn
We can now recursively define pathn:
ji
E,vvjijipath
jkpathkipathjipathjipath
ji
kkkk
if 0,
or , if ,1]][[
])][[]][[(]][[]][[
0
111
23
Warshall(graph_matrix G)1. n|V|,
2. for i1 to n
3. for j1 to n
4. if i=j OR <vi,vj> in E
5. then path0[i][j] 1, else 0
6. for k1 to n
7. for i1 to n
8. for j1 to n
9.
10. return pathn
])][[]][[(]][[]][[ 111 jkpathkipathjipathjipath kkkk