08 Graph Algorithms Part2
08 Graph Algorithms Part2
Graph Algorithms-Part2
Computer Science Dept.
Instructor: Ameera Jaradat
Outline
Greedy
MST
Kruskal’s
Prim’s
Single source shortest path
Dijkstra’s Algorithm
Dynamic
Warshall’s Algorithm: Transitive Closure
Floyd’s Algorithm: All pairs shortest paths
2
.a min-weight spanning tree (MST)
Given an undirected, connected graph (V, E) with edge weights find the
min-weight subset T ⊆ E such that the graph (V, T ) is acyclic and
connected.
Input:
a Weighted Undirected Graph G = (V,E,w): each edge (v, u) E has a
weight w(u, v).
Required:
Spanning tree E E connects all of the vertices of G
T G
weight of T w(T ) w (u , v )
is minimum
( u , v )T
Greedy Algorithms
Kruskal's algorithm. Start with T = . Consider edges in
ascending order of cost. Insert edge e in T unless doing so
would create a cycle.
Will make this O(|E| log |E|) via the union find data structure
Kruskal's algorithm
6
Prims Algorithm
Maintains a set of vertices VT already in the spanning tree
The tree starts from an arbitrary root vertex r and grows until
the tree spans all the vertices in VG
At each step, the min weight edge that is adjacent to the
already chosen nodes in VT is added to the tree.
Prims Algorithm
1. ET =
2. VT contains an arbitrary node.
3. Repeat
4. For each node adjacent to VT
1. V = Select-min(VG - VT)
5. If union(ET, (u,v) ) makes no cycle
Add-edge (u,v) to ET
Remove-node(V) from VG
Add-node(v) to VT
WT = WT + w (u,v)
Else discard that edge
Running time
6. Untill VG is empty
Using heaps we can solve in
7. Return ET O((|V| + |E|)log|V|)
mlogn
Prims Algorithm
9
Single-source shortest paths
Single-source shortest paths
G = (V, E) find a shortest path from a given source vertex s
to each vertex v V
Algorithm
Dijkstra’s algorithm
Shortest-Paths Notation
For each vertex v V:
d[v]: shortest-path weight
Initially, d[v]=∞
t x
6
3 9
3
4
2 1
s 0
2 7
5 3
5 11
6
y z
Relaxation Step
Relaxing an edge (u, v) = testing whether we can
improve the shortest path to v found so far by going
through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
d[v]=d[u]+w(u,v)
s
u v
2
5 9
After relaxation:
RELAX(u, v, w) d[v] d[u] + w(u, v)
u v
2
5 7
Dijkstra’s Algorithm
Maintains a set S of vertices whose SP from s has been determined.
Repeatedly selects u in V–S with minimum SP estimate (greedy choice).
Store V–S in priority queue Q.
Initialize(G, s);
S := ;
Q := V[G];
while Q do
u := Extract-Min(Q);
S := S {u}; Running time is
for each v Adj[u] do
Relax(u, v, w) O(V2) using linear array.
end O((V + E) lg V) using binary
end heap.
Example
u v
1
10
9
2 3
s 0 4 6
5 7
2
x y
Example
u v
1
10
10
9
2 3
s 0 4 6
5 7
5
2
x y
Example
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Warshall’s Algorithm: Transitive Closure
• Computes the transitive closure of a relation
• Alternatively: existence of all nontrivial paths in a digraph
• Example of transitive closure:
3 3
1 1
4 4 0 1 0 0
2 0 1 0 0 2
1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 1 0 1 1 1 1
Warshall’s Algorithm
Constructs transitive closure T as the last matrix in the sequence of n-by-n matrices
R(0), … , R(k), … , R(n) where
R(k )[i,j] = 1 iff there is nontrivial path from i to j with only the first k vertices
allowed as intermediate
Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
3 3 3 3 3
1 1 1 1 1
4 4 4 2 4 4
2 2 2 2
i j
R(k-1)(i,j)
On the k-th iteration, the algorithm determines for every pair of vertices i, j if a path
exists from i and j with just vertices 1,…,k allowed as intermediate
Warshall’s Algorithm (matrix generation)
Recurrence relating elements R(k) to elements of R(k-1) is:
3 0 1 0 0 0 1 0 0
1
1 0 0 1 1 1 0 1
=
R(0) 0 0 0 0 =
R(1) 0 0 0 0
2 4 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0 0
1 1 0 1 1 1 0 1 1 1 1 1
=
R(2) 0 0 0 0 =
R(3) 0 0 0 0 =
R(4) 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
Warshall’s Algorithm (pseudocode and analysis)
:Example
4 3
1 ∞ 4 ∞ 0
1 ∞3 4 0 1
6
1 5 ∞ 0 ∞
0 1 5 6
4
2 3
Floyd-Warshall recurrence
D(k-1)(i,k) + D(k-1)(k,j)
k
i j
D(k-1)(i,j)
Floyd’s Algorithm (example)
2
1 2 ∞ 3 ∞ 0 ∞ 3 ∞ 0
3 6 7 ∞ ∞ 0 2 ∞ 5 0 2
=
D(0) 1 0 7 ∞ =
D(1) 1 0 7 ∞
0 ∞ ∞ 6 0 9 ∞ 6
3 1 4
∞ 3 ∞ 0 4 3 10 0 4 3 10 0
∞ 5 0 2 6 5 0 2 6 5 0 2
=
D(2) 1 0 7 9 =
D(3) 1 0 7 9 =
D(4) 1 0 7 7
0 9 ∞ 6 0 9 16 6 0 9 16 6
Floyd’s Algorithm (pseudocode and analysis)