0% found this document useful (0 votes)
14 views27 pages

Minimum Cost Spanning Tree

Uploaded by

routaramesh76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

Minimum Cost Spanning Tree

Uploaded by

routaramesh76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Minimum Cost Spanning Tree

Spanning Trees
• Given (connected) graph G(V,E),
a spanning tree T(V’,E’):
– Is a subgraph of G; that is, V’  V, E’  E.
– Spans the graph (V’ = V)
– Forms a tree (no cycle);
– So, E’ has |V| -1 edges

2
Minimum Spanning Trees
• Edges are weighted: find minimum cost
spanning tree
• Applications
– Find cheapest way to wire your house
– Find minimum cost to send a message on the
Internet

11/01/2024 CSE 373 AU 04 - Minimum Spanning 3


Trees
Strategy for Minimum Spanning Tree

• For any spanning tree T, inserting an edge enew


not in T creates a cycle
• But
– Removing any edge eold from the cycle gives back a
spanning tree
– If enew has a lower cost than eold we have
progressed!

11/01/2024 CSE 373 AU 04 - Minimum Spanning 4


Trees
Strategy
• Strategy for construction:
– Add an edge of minimum cost that does not
create a cycle (greedy algorithm)
– Repeat |V| -1 times
– Correct since if we could replace an edge with one
of lower cost, the algorithm would have picked it
up

11/01/2024 CSE 373 AU 04 - Minimum Spanning 5


Trees
Two Algorithms
• Prim: (build tree incrementally)
– Pick lower cost edge connected to known (incomplete)
spanning tree that does not create a cycle and expand to
include it in the tree
• Kruskal: (build forest that will finish as a tree)
– Pick lowest cost edge not yet in a tree that does not create
a cycle. Then expand the set of included edges to include
it. (It will be somewhere in the forest.)

11/01/2024 CSE 373 AU 04 - Minimum Spanning 6


Trees
Prim’s algorithm
1
Starting from empty T,
10 5
choose a vertex at random
and initialize 1
V = {1), E’ ={}
8 3
2 3 4

1 1 6

4
2
6 5

11/01/2024 CSE 373 AU 04 - Minimum Spanning 7


Trees
Prim’s algorithm
1
Choose the vertex u not in V
10 5
such that edge weight from u to
a vertex in V is minimal (greedy!) 1
V={1,3} E’= {(1,3) }
8 3
2 3 4

1 1 6

4
2
6 5

11/01/2024 CSE 373 AU 04 - Minimum Spanning 8


Trees
Prim’s algorithm
Repeat until all vertices have been
1
chosen
10 5
Choose the vertex u not in V such that
edge weight from v to a vertex in V is 1
minimal (greedy!)
V= {1,3,4} E’= {(1,3),(3,4)} 8 3
2 3 4
V={1,3,4,5} E’={(1,3),(3,4),(4,5)}
…. 6
1 1
V={1,3,4,5,2,6}
E’={(1,3),(3,4),(4,5),(5,2),(2,6)} 4
2
6 5

11/01/2024 CSE 373 AU 04 - Minimum Spanning 9


Trees
Prim’s algorithm
Repeat until all vertices have been
1
chosen
10 5
V={1,3,4,5,2,6}
1
E’={(1,3),(3,4),(4,5),(5,2),(2,6)}

8 3
Final Cost: 1 + 3 + 4 + 1 + 1 = 10 2 3 4

1 1 6

4
2
6 5

11/01/2024 CSE 373 AU 04 - Minimum Spanning 10


Trees
Prim’s Algorithm:
1. Algorithm Prim(E, cost, n,t)
2. // E is the set of edges in G. Cost[1:n, 1:n] is the cost
3. // adjacency matrix of an n vertex graph such that cost[i,j]
is
4. //Either a positive real number or Ꝏ if no edge (i,j) exists.
5. // A minimum spanning tree is computed and stored as a
set of
6. // edges in the array t[1: n-1, 1:2]. (t[i,1], t[i,2]) is an edge
in the minimum-cost spanning tree.
7. // The final cost is returned.
Prim’s Algorithm:
8. {
9. Let (k,l) be an edge of minimum cost in E.
10. mincost= cost[k,l]
11. t[1,1]= k; t[1,2]= l
12. for i=1 to n do // initialize near
13. if (cost[i,l]< cost[i,k]) then near[i]= l;
14. else near[i]= k;
15. near[k]= near[l]= 0
16. for i=2 to n-1 do
17. {// Find n-2 additional edges for t
Prim’s Algorithm:
18. Let j be an index such that near[j]!=0 and
19. cost[j, near[j] is maximum;
20. T[i,1]= j; t[i,2]= near[j];
21. mincost= mincost+ cost[j, near[j]]
22. near[j]= 0;
23. for k=1 to n do // Update near[]
24. if ( (near [k]!=0) and (cost[k, near[k]]> cost[k,j]))
25. then near[k]= j
26. }
27. Return mincost
28. }
Krushkal’ Algorithm:
Krushkal’s Algorithm
Krushkal’s Algorithm
1. Algorithm Krushkal (E, cost, n,t)
2. // E is the set of edges in G. G has n vertices.
3. // cost[u,v] is the cost of edges (u,v). T is the //set of edges
in the maximum cost spanning
4. // tree. The final cost is returned.
5. {
6. Constructed a heap out of the edge costs using Heapify;
7. for i=1 to n do parent[i] = -1
8. // Each vertex is in a different set.
9. i= 0; mincost= 0.0;
Krushkal’s Algorithm
10. While (i<n-1) and (heap not empty) ) do
11. {
12. Delete a minimum cost edge (u,v) from the
heap
13. And reheapify using Adjust
14. j= find (u); k= find(v);
15. if(j!=k) then
16. {
17. i=i+1;
18. T[i,1]= u; t[1,2]= v
19. Mincost= mincost + cost[u,v]
Krushkal’s Algorithm
20. Union(j,k);
21. }
22. }
23. if(i!= n-1) then write (“ No Spanning Tree”)
24. Else return mincost;
25.}
SINGLE-SOURCE SHORTEST PATHS:
• Graphs can be used to represent the highway structure of a
state or country with vertices representing cities and edges
representing sections of highway.
• The edges can then be assigned weights which may be either
the distance between the two cities connected by the edge
or the average time to drive along that section of highway.
• A motorist wishing to drive from city A to B
would be interested in answers to the following questions:
a. Is there a path from A to B1?
b. If there is more than one path from A to B, which is the
shortest path?
SINGLE-SOURCESHORTESTPATHS:
SINGLE-SOURCESHORTESTPATHS:
SINGLE-SOURCE SHORTEST PATHS:
1. Algorithm Shortestpaths ( v, cost, dist, n)
2. // dist[j], 1<=j<=n, is set to the length of the shortest
3. // path from vertex v to vertex j in a digraph G with n
4. // Vertices dist[v] is set to zero. G is represented by its
5. // cost adjacency matrix cost[1:n, 1:n]
6. {
7. for i= 1 to n do
8. {// Initialize S.
9. S[i]= false; dist[i]= cost [ v,i];
10. }
SINGLE-SOURCESHORTESTPATHS:
11. S[v]= true; dist[v]= 0.0;// put v in S
12.For num= 2 to n-1 do
13. {
14. // Determine n-1 paths from v.
15. Choose u from among those vertices not
16.In S such that dist[u] is minimum.
17. S[u]= true; // Put u in S
18. for (each w adjacent to u with S[w] = false do
SINGLE-SOURCESHORTESTPATHS:
19.// Update distance
20. if ( dist[w] > dist[u] + cost[u,w]) then
21. dist[w]= dist[u] + cost[u,w];
22. }
23. }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy