SlideShare a Scribd company logo
Spanning Tree
What is A Spanning Tree?
• A spanning tree for an
undirected graph G=(V,E)
is a subgraph of G that is
a tree and contains all the
vertices of G

• Can a graph have more
than one spanning tree?
• Can an unconnected graph
have a spanning tree?

a
b

u

e

c

v

f

d
Minimal Spanning Tree.
• The weight of a subgraph
is the sum of the weights
of it edges.

a

4

9

3

b

• A minimum spanning tree
for a weighted graph is a
spanning tree with
minimum weight.

4

u

14

2
10

c

v

3

Mst T: w( T )=

15

f

8

d

• Can a graph have more
then one minimum
spanning tree?

e

(u,v)

T

w(u,v ) is minimized
Example of a Problem that
Translates into a MST

The Problem
• Several pins of an electronic circuit must be
connected using the least amount of wire.
Modeling the Problem
• The graph is a complete, undirected graph
G = ( V, E ,W ), where V is the set of pins, E
is the set of all possible interconnections
between the pairs of pins and w(e) is the
length of the wire needed to connect the
pair of vertices.
• Find a minimum spanning tree.
Greedy Choice
We will show two ways to build a minimum
spanning tree.
• A MST can be grown from the current
spanning tree by adding the nearest vertex
and the edge connecting the nearest
vertex to the MST. (Prim's algorithm)
• A MST can be grown from a forest of
spanning trees by adding the smallest edge
connecting two spanning trees. (Kruskal's
algorithm)
Notation
• Tree-vertices: in the tree constructed so far
• Non-tree vertices: rest of vertices

Prim’s Selection rule
• Select the minimum weight edge between a treenode and a non-tree node and add to the tree
The Prim algorithm Main Idea
Select a vertex to be a tree-node

while (there are non-tree vertices) {
if there is no edge connecting a tree node
with a non-tree node
return “no spanning tree”
select an edge of minimum weight between a
tree node and a non-tree node

add the selected edge and its new vertex to
the tree
}
return tree
5

A

4

6
2

C

2

E

3

D
1

3

B

2
4

Prim's Algorithm

F
A

B

D

C

E

Prim's Algorithm

F
A

C

2

B

D

E

F
Prim's Algorithm
5

A
4

2

6
2

C

B

D
1

3
E

2
4

Prim's Algorithm

F
A

B
2

2

C

1

3

3

D

E

2
F

Prim's Algorithm
A

B
2

2

C

1

3

3

D

E

2
F

Prim's Algorithm
A

B
2

2

C

D
1

3
E

2
F

Prim's Algorithm
A

B
2

2

C

D
1

3
E

2
F

Prim's Algorithm
minimum- spanning tree
A

B
2

2

C

D
1

3
E

2
F

Prim's Algorithm
Kruskal„s Algorithm
1. Each vertex is in its own cluster
2. Take the edge e with the smallest weight
- if e connects two vertices in different clusters,
then e is added to the MST and the two clusters,
which are connected by e, are merged into a
single cluster
- if e connects two vertices, which are already
in the same cluster, ignore it
3.

Continue until n-1 edges were selected
Kruskal's Algorithm
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F

cycle!!
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
5

A
4

6
2

C

2

E

3

D
1

3

B

2
4

Kruskal's Algorithm

F
minimum- spanning tree

A

B
2

2

C

D
1

3
E

2
F

Kruskal's Algorithm
Graph Traversal
Traversing a graph means visiting all the
vertices in the graph exactly once.
Breadth First Search (BFS)
Depth First Search (DFS)
DFS

Similar to in-order traversal of a binary
search tree
Starting from a given node, this traversal
visits all the nodes up to the deepest
level and so on.
v1

v2

v1

v8

v3

DFS

v2

v4
v6

v5
v7
DFS : V1

- V2

v8

v3

v4
v6

v5
v7

- V5 - V7 – V4 - V8 – V6 – V3
v1

v2

v1

v8

v3

v4
v6

v5
v7
DFS : V1

- V2

DFS

v2

v8

v3

v4

v6

v5
v7

- V5 - V7 – V4 - V8 – V3 – V6
DFS Traversal

Visit the vertex v
Visit all the vertices along the path which
begins at v
Visit the vertex v, then the vertex
immediate adjacent to v, let it be vx . If vx
has an immediate adjacent vy then visit it
and so on till there is a dead end.
Dead end: A vertex which does not have an
immediate adjacent or its immediate
adjacent has been visited.
After coming to an dead end we
backtrack to v to see if it has an
another adjacent vertex other than vx
and then continue the same from it else
from the adjacent of the adjacent
(which is not visited earlier) and so on.
Push the starting vertex into the STACK
While STACK not empty do
POP a vertex V
If V is not visited
Visit the vertex V
Store V in VISIT
PUSH all adjacent vertex of V
onto STACK
End of IF
End of While
STOP
A

Adjacency List

C

D
J

B

E

F

G
K

A: F,C,B
B: G,C
C: F
D: C
E: D,C,J
F: D
G: C,E
J: D,K
K: E,G
DFS of G starting at J
[1] Initially push J onto STACK
STACK : J
VISIT: Ø
[2] POP J from the STACK, add it in
VISIT and PUSH onto the STACK all
neighbor of J
STACK: D, K
VISIT: J
[3] POP the top element K, add it in
VISIT and PUSH all neighbor of K onto
STACK
STACK: D,E,G
VISIT: J, K
[4] POP the top element G, add it in
VISIT and PUSH all neighbor of G onto
STACK
STACK: D,E, E, C,
VISIT: J, K, G
[5] POP the top element C, add it in
VISIT and PUSH all neighbor of C onto
STACK
STACK: D,E,E, F
VISIT: J, K, G, C
[6] POP the top element F, add it in
VISIT and PUSH all neighbor of F onto
STACK
STACK: D,E, E, D
VISIT: J, K, G, C, F
[5] POP the top element D, add it in
VISIT and PUSH all neighbor of D onto
STACK
STACK: D,E,E, C
VISIT: J, K, G, C, F,D
[6] POP the top element C, which is
already in VISIT
STACK: D,E, E
VISIT: J, K, G, C, F,D
[5] POP the top element E, add it in
VISIT which is already in VISIT and
its neighbor onto STACK
STACK: D,E, D, C, J
VISIT: J, K, G, C, F,D,E
[6] POP the top element J, C, D,E, D
which is already in VISIT
STACK:
VISIT: J, K, G, C, F, D, E
A
C

D
J

B

E

F

J, K, G, C, F, D, E

Adjacency List

G
K

A: F,C,B
B: G,C
C: F
D: C
E: D,C,J
F: D
G: C,E
J: D,K
K: E,G
BFS Traversal
Any vertex in label i will be visited only
after the visiting of all the vertices in
its preceding level that is at level i – 1
BFS Traversal

[1] Enter the starting vertex v in a queue
Q
[2] While Q is not empty do
Delete an item from Q, say u
If u is not in VISIT store u in
VISIT
Enter all adjacent vertices of u
into Q
[3] Stop
v1

v2

v8

v3

v4

v6

v5
v7
[1] Insert the starting vertex V1 in Q
Q = V1
VISIT = Ø
[2] Delete an item from Q, let it be u = V1
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V2 , V 3
VISIT = V1
[3] Delete an item from Q, let it be u = V2
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V2 , V3 , V4 , V5
VISIT = V1 , V2
[4] Delete an item from Q, let it be u = V3
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V3 , V4 , V5 , V4 , V6
VISIT = V1 , V2 , V3
[5] Delete an item from Q, let it be u = V4
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V4 , V5 , V4 , V6 , V8
VISIT = V1 , V2 , V3 , V4
[6] Delete an item from Q, let it be u =V5
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V5 , V4 , V6 , V8 , V7
VISIT = V1 , V2 , V3 , V4 , V5
[7] Delete an item from Q, let it be u =V4
u is in VISIT.
Q = V4 , V6 , V8 , V7
VISIT = V1 , V2 , V3 , V4 , V5
[8] Delete an item from Q, let it be u =V6
u is not in VISIT. Store u in VISIT
and its adjacent element in Q
Q = V6 , V8 , V7
VISIT = V1 , V2 , V3 , V4 , V5 , V6
[9] Delete an item from Q, let it be u =V8
u is not in VISIT. Store u in VISIT and
its adjacent element in Q
Q = V8 , V7 , V1
VISIT = V1 , V2 , V3 , V4 , V5 , V6 ,
V8
[10] Delete an item from Q, let it be u =V7
u is not in VISIT. Store u in VISIT and
its adjacent element in Q
Q = V7 , V1
VISIT = V1 , V2 , V3 , V4 , V5 , V6 ,
V8 , V7
[11] Delete an item from Q, let it be u =V1
u is in VISIT.
Q = V1
VISIT = V1 , V2 , V3 , V4 , V5 , V6 ,
V8 , V 7
[12] Q is empty, Stop
Q=
VISIT = V1 , V2 , V3 , V4 , V5 , V6 ,
V8 , V 7
v1

v2

v1

v8

v8

v3

BFS

v2

v4

v6

v5
v7

v3

v4
v6

v5
v7

More Related Content

What's hot

Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
Mohammad Akbarizadeh
 
Network analysis
Network analysisNetwork analysis
Network analysis
Hakeem-Ur- Rehman
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Topological Sort
Topological SortTopological Sort
Topological Sort
Dr Sandeep Kumar Poonia
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
Dhaval Shukla
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
AJAL A J
 
Shortest path
Shortest pathShortest path
Shortest path
Farah Shaikh
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
Benjamin Sach
 
Conics parabola
Conics parabolaConics parabola
Conics parabola
sidi gaberson Empas
 
IRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump GraphsIRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump Graphs
IRJET Journal
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questionsSamet öztoprak
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
Samet öztoprak
 
Farhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning treeFarhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning tree
Farhana Shaikh
 
New Families of Odd Harmonious Graphs
New Families of Odd Harmonious GraphsNew Families of Odd Harmonious Graphs
New Families of Odd Harmonious Graphs
IJMIT JOURNAL
 
Day 3 vectors worked
Day 3   vectors workedDay 3   vectors worked
Day 3 vectors worked
Jonna Ramsey
 
Galois field
Galois fieldGalois field
Galois field
Niaj Morshed
 
Review Sheet A Substitution And Solving Inequalities
Review Sheet A Substitution And Solving InequalitiesReview Sheet A Substitution And Solving Inequalities
Review Sheet A Substitution And Solving Inequalitiesvmonacelli
 

What's hot (19)

Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
 
Network analysis
Network analysisNetwork analysis
Network analysis
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Linear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent SetLinear Combination, Span And Linearly Independent, Dependent Set
Linear Combination, Span And Linearly Independent, Dependent Set
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Shortest path
Shortest pathShortest path
Shortest path
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Conics parabola
Conics parabolaConics parabola
Conics parabola
 
IRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump GraphsIRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump Graphs
 
Linear equations 2-3
Linear equations   2-3Linear equations   2-3
Linear equations 2-3
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
 
Chap4
Chap4Chap4
Chap4
 
Farhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning treeFarhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning tree
 
New Families of Odd Harmonious Graphs
New Families of Odd Harmonious GraphsNew Families of Odd Harmonious Graphs
New Families of Odd Harmonious Graphs
 
Day 3 vectors worked
Day 3   vectors workedDay 3   vectors worked
Day 3 vectors worked
 
Galois field
Galois fieldGalois field
Galois field
 
Review Sheet A Substitution And Solving Inequalities
Review Sheet A Substitution And Solving InequalitiesReview Sheet A Substitution And Solving Inequalities
Review Sheet A Substitution And Solving Inequalities
 

Viewers also liked

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
Äshïsh Jäïn
 
Tree
TreeTree
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structureghhgj jhgh
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 

Viewers also liked (20)

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
12111 data structure
12111 data structure12111 data structure
12111 data structure
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Data structure
Data structureData structure
Data structure
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Tree
TreeTree
Tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar to Lecture 16 data structures and algorithms

Ppt 1
Ppt 1Ppt 1
19 primkruskal
19 primkruskal19 primkruskal
19 primkruskal
fika sweety
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
Krish_ver2
 
19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure
EMEY GUJJAR
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
JahidulIslam47153
 
Topological sort
Topological sortTopological sort
Topological sort
jabishah
 
Graph
GraphGraph
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to TreewidthASPAK2014
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
janafridi251
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
Mandeep Singh
 
Graphs
GraphsGraphs
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 
Cs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answersCs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answers
appasami
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR
 
Grpahs in Data Structure
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data Structure
AvichalVishnoi
 

Similar to Lecture 16 data structures and algorithms (20)

Graphs
GraphsGraphs
Graphs
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
19 primkruskal
19 primkruskal19 primkruskal
19 primkruskal
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
 
19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure19 prim,kruskal alg. in data structure
19 prim,kruskal alg. in data structure
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected Components
 
Topological sort
Topological sortTopological sort
Topological sort
 
Graph
GraphGraph
Graph
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Graphs
GraphsGraphs
Graphs
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
 
Cs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answersCs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answers
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Grpahs in Data Structure
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data Structure
 

Recently uploaded

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 

Lecture 16 data structures and algorithms

  • 2. What is A Spanning Tree? • A spanning tree for an undirected graph G=(V,E) is a subgraph of G that is a tree and contains all the vertices of G • Can a graph have more than one spanning tree? • Can an unconnected graph have a spanning tree? a b u e c v f d
  • 3. Minimal Spanning Tree. • The weight of a subgraph is the sum of the weights of it edges. a 4 9 3 b • A minimum spanning tree for a weighted graph is a spanning tree with minimum weight. 4 u 14 2 10 c v 3 Mst T: w( T )= 15 f 8 d • Can a graph have more then one minimum spanning tree? e (u,v) T w(u,v ) is minimized
  • 4. Example of a Problem that Translates into a MST The Problem • Several pins of an electronic circuit must be connected using the least amount of wire. Modeling the Problem • The graph is a complete, undirected graph G = ( V, E ,W ), where V is the set of pins, E is the set of all possible interconnections between the pairs of pins and w(e) is the length of the wire needed to connect the pair of vertices. • Find a minimum spanning tree.
  • 5. Greedy Choice We will show two ways to build a minimum spanning tree. • A MST can be grown from the current spanning tree by adding the nearest vertex and the edge connecting the nearest vertex to the MST. (Prim's algorithm) • A MST can be grown from a forest of spanning trees by adding the smallest edge connecting two spanning trees. (Kruskal's algorithm)
  • 6. Notation • Tree-vertices: in the tree constructed so far • Non-tree vertices: rest of vertices Prim’s Selection rule • Select the minimum weight edge between a treenode and a non-tree node and add to the tree
  • 7. The Prim algorithm Main Idea Select a vertex to be a tree-node while (there are non-tree vertices) { if there is no edge connecting a tree node with a non-tree node return “no spanning tree” select an edge of minimum weight between a tree node and a non-tree node add the selected edge and its new vertex to the tree } return tree
  • 17. Kruskal„s Algorithm 1. Each vertex is in its own cluster 2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a single cluster - if e connects two vertices, which are already in the same cluster, ignore it 3. Continue until n-1 edges were selected Kruskal's Algorithm
  • 27. Graph Traversal Traversing a graph means visiting all the vertices in the graph exactly once. Breadth First Search (BFS) Depth First Search (DFS)
  • 28. DFS Similar to in-order traversal of a binary search tree Starting from a given node, this traversal visits all the nodes up to the deepest level and so on.
  • 29. v1 v2 v1 v8 v3 DFS v2 v4 v6 v5 v7 DFS : V1 - V2 v8 v3 v4 v6 v5 v7 - V5 - V7 – V4 - V8 – V6 – V3
  • 30. v1 v2 v1 v8 v3 v4 v6 v5 v7 DFS : V1 - V2 DFS v2 v8 v3 v4 v6 v5 v7 - V5 - V7 – V4 - V8 – V3 – V6
  • 31. DFS Traversal Visit the vertex v Visit all the vertices along the path which begins at v Visit the vertex v, then the vertex immediate adjacent to v, let it be vx . If vx has an immediate adjacent vy then visit it and so on till there is a dead end. Dead end: A vertex which does not have an immediate adjacent or its immediate adjacent has been visited.
  • 32. After coming to an dead end we backtrack to v to see if it has an another adjacent vertex other than vx and then continue the same from it else from the adjacent of the adjacent (which is not visited earlier) and so on.
  • 33. Push the starting vertex into the STACK While STACK not empty do POP a vertex V If V is not visited Visit the vertex V Store V in VISIT PUSH all adjacent vertex of V onto STACK End of IF End of While STOP
  • 34. A Adjacency List C D J B E F G K A: F,C,B B: G,C C: F D: C E: D,C,J F: D G: C,E J: D,K K: E,G
  • 35. DFS of G starting at J [1] Initially push J onto STACK STACK : J VISIT: Ø [2] POP J from the STACK, add it in VISIT and PUSH onto the STACK all neighbor of J STACK: D, K VISIT: J
  • 36. [3] POP the top element K, add it in VISIT and PUSH all neighbor of K onto STACK STACK: D,E,G VISIT: J, K [4] POP the top element G, add it in VISIT and PUSH all neighbor of G onto STACK STACK: D,E, E, C, VISIT: J, K, G
  • 37. [5] POP the top element C, add it in VISIT and PUSH all neighbor of C onto STACK STACK: D,E,E, F VISIT: J, K, G, C [6] POP the top element F, add it in VISIT and PUSH all neighbor of F onto STACK STACK: D,E, E, D VISIT: J, K, G, C, F
  • 38. [5] POP the top element D, add it in VISIT and PUSH all neighbor of D onto STACK STACK: D,E,E, C VISIT: J, K, G, C, F,D [6] POP the top element C, which is already in VISIT STACK: D,E, E VISIT: J, K, G, C, F,D
  • 39. [5] POP the top element E, add it in VISIT which is already in VISIT and its neighbor onto STACK STACK: D,E, D, C, J VISIT: J, K, G, C, F,D,E [6] POP the top element J, C, D,E, D which is already in VISIT STACK: VISIT: J, K, G, C, F, D, E
  • 40. A C D J B E F J, K, G, C, F, D, E Adjacency List G K A: F,C,B B: G,C C: F D: C E: D,C,J F: D G: C,E J: D,K K: E,G
  • 41. BFS Traversal Any vertex in label i will be visited only after the visiting of all the vertices in its preceding level that is at level i – 1
  • 42. BFS Traversal [1] Enter the starting vertex v in a queue Q [2] While Q is not empty do Delete an item from Q, say u If u is not in VISIT store u in VISIT Enter all adjacent vertices of u into Q [3] Stop
  • 44. [1] Insert the starting vertex V1 in Q Q = V1 VISIT = Ø [2] Delete an item from Q, let it be u = V1 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V2 , V 3 VISIT = V1
  • 45. [3] Delete an item from Q, let it be u = V2 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V2 , V3 , V4 , V5 VISIT = V1 , V2 [4] Delete an item from Q, let it be u = V3 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V3 , V4 , V5 , V4 , V6 VISIT = V1 , V2 , V3
  • 46. [5] Delete an item from Q, let it be u = V4 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V4 , V5 , V4 , V6 , V8 VISIT = V1 , V2 , V3 , V4 [6] Delete an item from Q, let it be u =V5 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V5 , V4 , V6 , V8 , V7 VISIT = V1 , V2 , V3 , V4 , V5
  • 47. [7] Delete an item from Q, let it be u =V4 u is in VISIT. Q = V4 , V6 , V8 , V7 VISIT = V1 , V2 , V3 , V4 , V5 [8] Delete an item from Q, let it be u =V6 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V6 , V8 , V7 VISIT = V1 , V2 , V3 , V4 , V5 , V6
  • 48. [9] Delete an item from Q, let it be u =V8 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V8 , V7 , V1 VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 [10] Delete an item from Q, let it be u =V7 u is not in VISIT. Store u in VISIT and its adjacent element in Q Q = V7 , V1 VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V7
  • 49. [11] Delete an item from Q, let it be u =V1 u is in VISIT. Q = V1 VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V 7 [12] Q is empty, Stop Q= VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V 7