SlideShare a Scribd company logo
1 of 16
Graphs and
Spanning Trees
What is a graph?
Seattle
New York
L.A.
Tokyo
Sydney
Seoul 128
140
181
30
16
56
Graphs
graph: A data structure containing:
a set of vertices V, (sometimes called nodes)
a set of edges E, where an edge
represents a connection between 2 vertices.
Graph G = (V, E)
an edge is a pair (v, w) where v, w are in V
the graph at right:
V = {a, b, c, d}
E = {(a, c), (b, c), (b, d), (c, d)}
degree: number of edges touching a given vertex.
at right: a=1, b=2, c=3, d=2
a
c
b
d
Graph examples
For each, what are the vertices and what are the edges?
Web pages with links
Methods in a program that call each other
Road maps (e.g., Google maps)
Airline routes
Facebook friends
Course pre-requisites
Family trees
Paths through a maze
Paths
path: A path from vertex a to b is a sequence of edges that can be
followed starting from a to reach b.
can be represented as vertices visited, or edges taken
example, one path from V to Z: {b, h} or {V, X, Z}
What are two paths from U to Y?
path length: Number of vertices
or edges contained in the path.
neighbor or adjacent: Two vertices
connected directly by an edge.
example: V and X
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
Weighted graphs
weight: Cost associated with a given edge.
Some graphs have weighted edges, and some are unweighted.
Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1,
etc.)
Most graphs do not allow negative weights.
example: graph of airline flights, weighted by miles between cities:
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
Directed graphs
directed graph ("digraph"): One where edges are one-way
connections between vertices.
If graph is directed, a vertex has a separate in/out degree.
A digraph can be weighted or unweighted.
Is the graph below connected? Why or why not?
a
d
b
e
g
f
c
Spanning trees
๏ฎ Suppose you have a connected undirected graph
๏ฎ
๏ฎ
Connected: every node is reachable from every other node.
Undirected: edges do not have an associated direction
๏ฎ
๏ฎ
๏ฎ
๏ฎ
...thus in the Mathematical field of graph theory, a spanning tree of the graph is
a connected sub-graph in which there are no cycles.
A Spanning tree for a graph has no cycles but still connects to every house.
If G is a connected graph with n vertices and m edges, spanning tree of G must
have n-1 edges, and no. of edges deleted from G to get a spanning tree must be
m-(n-1)=m-n+1.
A Graph may have many spanning trees; for instance the complete graph on
four vertices.
A connected,
undirected graph
Four of the spanning trees of the graph
8
Finding Spanning Trees
Spanning trees can be found in linear time by simply
performing breadth-first search or depth-first search.
These graph search algorithms are only dependent on
the number of vertices in the graph, so they are quite
fast.
Breadth-first search will use a queue to hold vertices to
explore later.
depth-first search will use a stack.
In either case, a spanning tree can be constructed by
connecting each vertex with the vertex that was used to
discover it.
Unfortunately, search algorithms are not well suited for
parallel or distributed computing, an area in which
spanning trees are popular.
There are, however, algorithms that are designed to find
spanning trees in a parallel setting.
4
Finding a spanning tree
๏ฎ To find a spanning tree of a graph,
Pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree, add to the
spanning tree both the new node and the edge you followed to get to it.
An undirected graph One possible
result of a BFS
starting from top
One possible
result of a DFS
starting from top
Properties of a spanning tree
There are a few general properties of spanning trees.
A connected graph can have more than one spanning tree.
All possible spanning trees for a graph G have the same number of edges and
vertices.
Spanning trees do not have any cycles.
Spanning trees are all minimally connected. That is, if any one edge is
removed, the spanning tree will no longer be connected.
Adding any edge to the spanning tree will create a cycle. So, a spanning tree is
maximally acyclic.
Spanning trees have |n|-1 edges, where |n| is the number of vertices
BFS
Spanningtreesppt
Spanningtreesppt

More Related Content

What's hot

Adjacency list
Adjacency listAdjacency list
Adjacency list
Stefi Yu
ย 
Graph representation
Graph representationGraph representation
Graph representation
Tech_MX
ย 
Data handling
Data handlingData handling
Data handling
Punita Verma
ย 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
Bianca TeลŸilฤƒ
ย 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphs
Tech_MX
ย 

What's hot (20)

Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
ย 
Adjacency list
Adjacency listAdjacency list
Adjacency list
ย 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
ย 
Graph representation
Graph representationGraph representation
Graph representation
ย 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
ย 
Types of graphs
Types of graphsTypes of graphs
Types of graphs
ย 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
ย 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
ย 
Graph: Euler path and Euler circuit
Graph: Euler path and Euler circuitGraph: Euler path and Euler circuit
Graph: Euler path and Euler circuit
ย 
Par 4.1(1)
Par 4.1(1)Par 4.1(1)
Par 4.1(1)
ย 
Data handling
Data handlingData handling
Data handling
ย 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
ย 
Basics of graph
Basics of graphBasics of graph
Basics of graph
ย 
Graphss
GraphssGraphss
Graphss
ย 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphs
ย 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
ย 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
ย 
Data handling class 8th
Data handling class 8thData handling class 8th
Data handling class 8th
ย 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
ย 
GRAPH THEORY
GRAPH THEORYGRAPH THEORY
GRAPH THEORY
ย 

Similar to Spanningtreesppt

Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
ย 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar
ย 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
ย 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
ย 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
zukun
ย 

Similar to Spanningtreesppt (20)

Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
ย 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
ย 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
ย 
Graphs
GraphsGraphs
Graphs
ย 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
ย 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
ย 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
ย 
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATIONFREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
FREQUENT SUBGRAPH MINING ALGORITHMS - A SURVEY AND FRAMEWORK FOR CLASSIFICATION
ย 
graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________graph_theory_1-11.pdf___________________
graph_theory_1-11.pdf___________________
ย 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
ย 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptx
ย 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
ย 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
ย 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
ย 
Graph therory
Graph theroryGraph therory
Graph therory
ย 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
ย 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
ย 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
ย 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
ย 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
ย 

Recently uploaded

Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
ย 

Recently uploaded (20)

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ย 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
ย 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
ย 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
ย 

Spanningtreesppt

  • 2. What is a graph? Seattle New York L.A. Tokyo Sydney Seoul 128 140 181 30 16 56
  • 3. Graphs graph: A data structure containing: a set of vertices V, (sometimes called nodes) a set of edges E, where an edge represents a connection between 2 vertices. Graph G = (V, E) an edge is a pair (v, w) where v, w are in V the graph at right: V = {a, b, c, d} E = {(a, c), (b, c), (b, d), (c, d)} degree: number of edges touching a given vertex. at right: a=1, b=2, c=3, d=2 a c b d
  • 4. Graph examples For each, what are the vertices and what are the edges? Web pages with links Methods in a program that call each other Road maps (e.g., Google maps) Airline routes Facebook friends Course pre-requisites Family trees Paths through a maze
  • 5. Paths path: A path from vertex a to b is a sequence of edges that can be followed starting from a to reach b. can be represented as vertices visited, or edges taken example, one path from V to Z: {b, h} or {V, X, Z} What are two paths from U to Y? path length: Number of vertices or edges contained in the path. neighbor or adjacent: Two vertices connected directly by an edge. example: V and X X U V W Z Y a c b e d f g h
  • 6. Weighted graphs weight: Cost associated with a given edge. Some graphs have weighted edges, and some are unweighted. Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1, etc.) Most graphs do not allow negative weights. example: graph of airline flights, weighted by miles between cities: ORD PVD MIA DFW SFO LAX LGA HNL
  • 7. Directed graphs directed graph ("digraph"): One where edges are one-way connections between vertices. If graph is directed, a vertex has a separate in/out degree. A digraph can be weighted or unweighted. Is the graph below connected? Why or why not? a d b e g f c
  • 8. Spanning trees ๏ฎ Suppose you have a connected undirected graph ๏ฎ ๏ฎ Connected: every node is reachable from every other node. Undirected: edges do not have an associated direction ๏ฎ ๏ฎ ๏ฎ ๏ฎ ...thus in the Mathematical field of graph theory, a spanning tree of the graph is a connected sub-graph in which there are no cycles. A Spanning tree for a graph has no cycles but still connects to every house. If G is a connected graph with n vertices and m edges, spanning tree of G must have n-1 edges, and no. of edges deleted from G to get a spanning tree must be m-(n-1)=m-n+1. A Graph may have many spanning trees; for instance the complete graph on four vertices. A connected, undirected graph Four of the spanning trees of the graph 8
  • 9. Finding Spanning Trees Spanning trees can be found in linear time by simply performing breadth-first search or depth-first search. These graph search algorithms are only dependent on the number of vertices in the graph, so they are quite fast. Breadth-first search will use a queue to hold vertices to explore later. depth-first search will use a stack. In either case, a spanning tree can be constructed by connecting each vertex with the vertex that was used to discover it.
  • 10. Unfortunately, search algorithms are not well suited for parallel or distributed computing, an area in which spanning trees are popular. There are, however, algorithms that are designed to find spanning trees in a parallel setting.
  • 11. 4 Finding a spanning tree ๏ฎ To find a spanning tree of a graph, Pick an initial node and call it part of the spanning tree do a search from the initial node: each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it. An undirected graph One possible result of a BFS starting from top One possible result of a DFS starting from top
  • 12. Properties of a spanning tree There are a few general properties of spanning trees. A connected graph can have more than one spanning tree. All possible spanning trees for a graph G have the same number of edges and vertices. Spanning trees do not have any cycles. Spanning trees are all minimally connected. That is, if any one edge is removed, the spanning tree will no longer be connected. Adding any edge to the spanning tree will create a cycle. So, a spanning tree is maximally acyclic. Spanning trees have |n|-1 edges, where |n| is the number of vertices
  • 13.
  • 14. BFS