0% found this document useful (0 votes)
81 views51 pages

Csn-261 (Data Structures Laboratory)

The document discusses algorithms for finding minimum spanning trees (MST) in weighted graphs, including Kruskal's and Prim's algorithms. It provides pseudocode for Kruskal's algorithm, which uses a disjoint-set data structure to build up an MST by successively connecting components with the lowest weight edge that does not create a cycle. Applications of MST including wiring problems and content distribution networks are also covered.

Uploaded by

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

Csn-261 (Data Structures Laboratory)

The document discusses algorithms for finding minimum spanning trees (MST) in weighted graphs, including Kruskal's and Prim's algorithms. It provides pseudocode for Kruskal's algorithm, which uses a disjoint-set data structure to build up an MST by successively connecting components with the lowest weight edge that does not create a cycle. Applications of MST including wiring problems and content distribution networks are also covered.

Uploaded by

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

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

CSN-261 (DATA STRUCTURES LABORATORY)


Lab Assignment 5: Polynomials, Kruskal’s and Prim’s Algorithms,
Weighted Graph, MSTs, STL, GraphViz, ETE Toolkit
Dr. Sudip Roy
Assistant Professor
Department of Computer Science and Engineering

Piazza Class Room: https://piazza.com/iitr.ac.in/fall2019/csn261


[Access Code: csn261_2019]
Moodle Submission Site: https://moodle.iitr.ac.in/course/view.php?id=46
[Enrollment Key: csn261_2019]
Lab Assignment:

2
Lab Assignment:

3
Lab Assignment:

4
Lab Assignment:

5
Lab Assignment:

6
Lab Assignment:

7
Lab Assignment:

8
Lab Assignment:

9
Lab Assignment:

10
Lab Assignment:

11
Algebra of Polynomials:

12
Algebra of Polynomials:

13
Algebra of Polynomials:

Addition of two polynomials:

(4 x 4 + 3 y 2 + 6 y ) − (2 x 4 + 2 y 2 + y)
= 4 x 4 + 3 y 2 + 6 y − 2 x 4 − 2 y 2− y
= (4 x 4 − 2 x 4) + (3 y 2 − 2 y 2) + (6 y – y)
= 2 x 4 + y2 + 5 y

14
Algebra of Polynomials:

Multiplication of two polynomials:

(2 y 3 + 3 x ) × (5 x 2 + 2 x )
= (2 y 3 × (5 x 2 + 2 x )) + (3 x × (5 x 2 + 2 x ))
= (2 y 3 × 5 x 2) + (2 y 3 × 2 x ) + (3 x × 5 x 2) + (3 x × 2 x )
= 10 y 3x2 + 4 y 3x + 15 x 3 + 6 x 2
15
Polynomial ADT using Linked List:

16
Weighted Graphs:

17
Weighted Graph ADT:

• Weighted Graph ADT


– Easy to modify the graph ADT(s) representations to accommodate
weights
– Also need to add operations to modify/inspect weights
• For example we can modify adjacency matrix representation
so entries in array are now numbers (int or float) rather than
true/false

• Refer to Book by Ellis Horowitz and Sartaj Sahni


– Weighted Edge Class
– Weighted Graph Class

18
Minimum Spanning Tree (MST):

• A Minimum Spanning Tree (MST) is a subgraph of an


undirected graph such that the subgraph spans (includes) all
nodes, is connected, is acyclic, and has minimum total edge
weight

• Both Prim’s and Kruskal’s Algorithms work with undirected


graphs
• Both work with weighted and unweighted graphs but are
more interesting when edges are weighted
• Both are greedy algorithms that produce optimal solutions

19
How Can We Generate a MST?

9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4

5 e 5 e
c c

20
Minimum Spanning Tree (MST)
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that

• it is a tree (i.e., it is acyclic)


• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the
minimum among all possible spanning trees
• not necessarily unique

21
Applications of MST
• Any time you want to visit all vertices in a graph at minimum
cost (e.g., wire routing on printed circuit boards, sewer pipe
layout, road planning…)

• Internet content distribution


– $$$, also a hot research topic
– Idea: publisher produces web pages, content distribution network
replicates web pages to many locations so consumers can access at
higher speed
– MST may not be good enough!
• content distribution on minimum cost tree may take a long time!

• Provides a heuristic for traveling salesman problems. The


optimum traveling salesman tour is at most twice the length
of the minimum spanning tree.
22
Problem: Laying Telephone Wire

Central office

23
Wiring: Naïve Approach

Central office

Expensive!

24
Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers

25
Application of Minimum Spanning Tree (MST)

• In the design of electronic circuitry, it is


often necessary to make a set of pins
electrically equivalent by wiring them
together.
• To interconnect n pins, we can use n-1
wires, each connecting two pins.
• We want to minimize the total length of
the wires.
• Minimum Spanning Trees can be used to
model this problem.

26
Minimum Connector Algorithms

Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in a 1. Select any vertex


network
2. Select the shortest edge
2. Select the next shortest edge connected to that vertex
which does not create a cycle
3. Select the shortest edge
3. Repeat step 2 until all vertices connected to any vertex
have been connected already connected

4. Repeat step 3 until all


vertices have been
connected

27
Kruskal’s Algorithm:

• Starts with each vertex in its own component.


• Repeatedly merges two components into one by choosing a
light edge that connects them (i.e., a light edge crossing the
cut between them).
• Scans the set of edges in monotonically increasing order by
weight.
• Uses a disjoint-set data structure to determine whether an
edge connects vertices in different components.

28
Kruskal's algorithm(basic part)

1 (Sort the edges in an increasing order)


2 A:={}
3 while E is not empty do {
3 take an edge (u, v) that is shortest in E
and delete it from E
4 if u and v are in different components then
add (u, v) to A
}
Note: each time a shortest edge in E is considered.

29
Kruskal's algorithm
MST_KRUSKAL(G,w)
1 A:={}
2 for each vertex v in V[G]
3 do MAKE_SET(v)
4 sort the edges of E by nondecreasing weight w
5 for each edge (u,v) in E, in order by nondecreasing weight
6 do if FIND_SET(u) != FIND_SET(v)
7 then A:=A∪{(u,v)}
8 UNION(u,v)
9 return A

30
Disjoint-Set

• Keep a collection of sets S1, S2, .., Sk,


– Each Si is a set, e,g, S1={v1, v2, v8}.
• Three operations
– Make-Set(x)-creates a new set whose only member is x.
– Union(x, y) –unites the sets that contain x and y, say, Sx
and Sy, into a new set that is the union of the two sets.
– Find-Set(x)-returns a pointer to the representative of the
set containing x.
– Each operation takes O(log n) time.

31
Union-Find:

• Our implementation uses a disjoint-set data structure to


maintain several disjoint sets of elements.
• Each set contains the vertices in a tree of the current forest.
• The operation FIND_SET(u) returns a representative
element from the set that contains u.
• Thus, we can determine whether two vertices u and v belong
to the same tree by testing whether FIND_SET(u) equals
FIND_SET(v).
• The combining of trees is accomplished by the UNION
procedure.
• Running time O(|E| lg (|E|)).

32
The execution of Kruskal's algorithm (Moderate part)

•The edges are considered by the algorithm in sorted order by weight.


•The edge under consideration at each step is shown with a red weight
number.

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

33
33
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

34
34
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

35
35
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

36
36
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

37
37
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

38
38
Prim’s Algorithm:
MST_PRIM(G,w,r)
1. A={}
2. S:={r} (r is an arbitrary node in V)
3. Q=V-{r};
4. while Q is not empty do {
5 take an edge (u, v) such that (1) u ∈S and v ∈ Q (v∉ S ) and
(u, v) is the shortest edge satisfying (1)
6 add (u, v) to A, add v to S and delete v from Q
}

39
Prim’s Algorithm
Q := V[G];
for each u ∈ Q do Complexity:
key[u] := ∞ Using binary heaps: O(E lg V).
od; Initialization – O(V).
key[r] := 0; Building initial queue – O(V).
π[r] := NIL; V Extract-Min’s – O(V lgV).
while Q ≠ ∅ do E Decrease-Key’s – O(E lg V).
u := Extract - Min(Q);
for each v ∈ Adj[u] do Using Fibonacci heaps: O(E + V lg V).
if v ∈ Q ∧ w(u, v) < key[v] then (see book)
π[v] := u;
key[v] := w(u, v)  decrease-key operation
fi
od
od

Note: A = {(v, π[v]) : v ∈ v - {r} - Q}.


Prim’s Algorithm:

• Grow the minimum spanning tree from the root vertex r.


• Q is a priority queue, holding all vertices that are not in the
tree now.
• key[v] is the minimum weight of any edge connecting v to a
vertex in the tree.
• parent[v] names the parent of v in the tree.
• When the algorithm terminates, Q is empty; the minimum
spanning tree A for G is thus A={(v,parent[v]):v∈V-{r}}.
• Running time: O(||E||lg |V|).

41
The execution of Prim's algorithm(moderate part)
8 7
the root b c d 9
vertex 4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
42
42
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

43
43
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

44
44
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

45
45
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2

Bottleneck spanning tree: A spanning tree of G whose largest edge weight is


minimum over all spanning trees of G. The value of the bottleneck spanning tree is
the weight of the maximum-weight edge in T.
Theorem: A minimum spanning tree is also a bottleneck spanning tree.
(Challenge problem)

46
46
GraphViz Tool:

47
GraphViz Tool:

48
ETE Toolkit:

49
ETE Toolkit:

50
Happy Coding…

51

You might also like