SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
Tech_MX
 
Discrete mathematic answers of questions
Discrete mathematic answers of questionsDiscrete mathematic answers of questions
Discrete mathematic answers of questions
Samet öztoprak
 
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
vmonacelli
 

Mais procurados (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
 

Destaque

Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
Aakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
Aakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
Aakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
Aakash deep Singhal
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
Aakash deep Singhal
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

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

Semelhante a Lecture 16 data structures and algorithms

Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
ASPAK2014
 

Semelhante a 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
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Último (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.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