SlideShare une entreprise Scribd logo
1  sur  50
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

Contenu connexe

Tendances

Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest pathMohammad Akbarizadeh
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
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 SetDhaval Shukla
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph AJAL A J
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum QueriesBenjamin Sach
 
IRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump GraphsIRJET-Entire Domination in Jump Graphs
IRJET-Entire Domination in Jump GraphsIRJET 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 questionsSamet öztoprak
 
Farhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning treeFarhana shaikh webinar_spanning tree
Farhana shaikh webinar_spanning treeFarhana Shaikh
 
New Families of Odd Harmonious Graphs
New Families of Odd Harmonious GraphsNew Families of Odd Harmonious Graphs
New Families of Odd Harmonious GraphsIJMIT JOURNAL
 
Day 3 vectors worked
Day 3   vectors workedDay 3   vectors worked
Day 3 vectors workedJonna Ramsey
 
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
 

Tendances (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
 

En vedette

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
 
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 treeZaid 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 STRUCTURESbca2010
 

En vedette (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
 

Similaire à Lecture 16 data structures and algorithms

2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demoKrish_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 structureEMEY GUJJAR
 
topological_sort_strongly Connected Components
topological_sort_strongly Connected Componentstopological_sort_strongly Connected Components
topological_sort_strongly Connected ComponentsJahidulIslam47153
 
Topological sort
Topological sortTopological sort
Topological sortjabishah
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to TreewidthASPAK2014
 
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.pptxzerihunnana
 
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 answersappasami
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6DrkhanchanaR
 
Grpahs in Data Structure
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data StructureAvichalVishnoi
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...ijceronline
 

Similaire à 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
 
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
 
Data structure
Data structureData structure
Data structure
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 

Dernier

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 

Dernier (20)

DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.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