SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptprithivr1
 
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
 

Ähnlich wie 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
 
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
 

Kürzlich hochgeladen

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 

Kürzlich hochgeladen (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 

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