Csn-261 (Data Structures Laboratory)
Csn-261 (Data Structures Laboratory)
Csn-261 (Data Structures Laboratory)
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:
(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:
(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:
18
Minimum Spanning Tree (MST):
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
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…)
Central office
23
Wiring: Naïve Approach
Central office
Expensive!
24
Wiring: Better Approach
Central office
25
Application of Minimum Spanning Tree (MST)
26
Minimum Connector Algorithms
27
Kruskal’s Algorithm:
28
Kruskal's algorithm(basic part)
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
31
Union-Find:
32
The execution of Kruskal's algorithm (Moderate part)
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
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
46
46
GraphViz Tool:
47
GraphViz Tool:
48
ETE Toolkit:
49
ETE Toolkit:
50
Happy Coding…
51