Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

CS251-Algorithms

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.

 Prim's algorithm. Start with some root node s and greedily


grow a tree T from s outward. At each step, add the cheapest
edge e to T that has exactly one endpoint in T.

 Remark. both algorithms produce an MST.


Kruskal Algorithm
1. ET = 
2. Sort EG in increasing order
by weight w
3. For each edge (u,v) in the
sorted list
If union(ET, (u,v) ) makes no
cycle
Add (u,v) to ET
WT = WT + w (u,v)
Else discard that edge
4.Running
Return ET;
time:
Sorting: O(|E| log |E|)
Checking if acyclic: |E| checks and each is O(1) time.
Adding e to ET : Updating array takes O(|V|) time and array is
updated |V| times.
Total Running Time: O(|E| log |E| + |V|2)

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]=∞

These values are changed when an edge (u, v) is relaxed:

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

R(0) R(1) R(2) R(3) R(4)


0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1
Warshall’s Algorithm (recurrence)

R(k)(i,j) = { R(k-1)(i,j) or [ R(k-1)(i,k) & R(k-1)(k,j) ]}


R(k-1)(i,k) & R(k-1)(k,j)
k

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:

R(k)[i,j] = R(k-1)[i,j] or (R(k-1)[i,k] and R(k-1)[k,j])

It implies the following rules for generating R(k) from R(k-1):

Rule 1 If an element in row i and column j is 1 in R(k-1),


it remains 1 in R(k)

Rule 2 If an element in row i and column j is 0 in R(k-1),


it has to be changed to 1 in R(k) if and only if
the element in its row i and column k and the element
in its column j and row k are both 1’s in R(k-1)
Warshall’s Algorithm (example)

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)

Time efficiency: Θ(n3)


.Space efficiency: Θ(n2)
Floyd’s Algorithm: All pairs shortest paths

Problem: In a weighted (di)graph, find shortest paths between


every pair of vertices

Same idea: construct solution through series of matrices D(0), …,


D (n) using increasing subsets of the vertices allowed
as intermediate

: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)(i,j) = min{ D(k-1)(i,j) , D(k-1)(i,k)+ D(k-1)(k,j)}

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)

Time efficiency: Θ(n3)


Space efficiency: Θ(n2)

You might also like