SlideShare a Scribd company logo
Lecture Notes on
                         Spanning Trees

            15-122: Principles of Imperative Computation
                           Frank Pfenning

                                Lecture 26
                               April 26, 2011


1   Introduction
In this lecture we introduce graphs. Graphs provide a uniform model for
many structures, for example, maps with distances or Facebook relation-
ships. Algorithms on graphs are therefore important to many applications.
They will be a central subject in the algorithms courses later in the curricu-
lum; here we only provide a very small sample of graph algorithms.


2   Spanning Trees
We start with undirected graphs which consist of a set V of vertices (also
called nodes) and a set E of edges, each connecting two different vertices.
A graph is connected if we can reach any vertex from any other vertex
by following edges in either direction. In a directed graph edges provide a
connection from one node to another, but not necessarily in the opposite
direction. More mathematically, we say that the edge relation between ver-
tices is symmetric for undirected graphs. In this lecture we only discuss
undirected graphs, although directed graphs also play an important role in
many applications.
    The following is a simple example of a connected, undirected graph




L ECTURE N OTES                                                A PRIL 26, 2011
Spanning Trees                                                             L26.2


with 5 vertices (A, B, C, D, E) and 6 edges (AB, BC, CD, AE, BE, CE).



                          A
              D

                                    E



                          B
              C




In this lecture we are particularly interested in the problem of computing
a spanning tree for a connected graph. What is a tree here? They are a
bit different than the binary search trees we considered early. One simple
definition is that a tree is a connected graph with no cycles, where a cycle let’s
you go from a node to itself without repeating an edge. A spanning tree for
a connected graph G is a tree containing all the vertices of G. Below are
two examples of spanning trees for our original example graph.



                    A
               D
        A
               D

                               E
                        E



                    B
               C
        B
               C




    When dealing with a new kind of data structure, it is a good strategy to
try to think of as many different characterization as we can. This is some-
what similar to the problem of coming up with good representations of the
data; different ones may be appropriate for different purposes. Here are
some alternative characterizations the class came up with:

   1. Connected graph with no cycle (original).

   2. Connected graph where no two neighbors are otherwise connected.
      Neighbors are vertices connected directly by an edge, otherwise con-
      nected means connected without the connecting edge.


L ECTURE N OTES                                                  A PRIL 26, 2011
Spanning Trees                                                             L26.3


    3. Two trees connected by a single edge. This is a recursive characteriza-
       tion. The based case is a single node, with the empty tree (no vertices)
       as a possible special case.

    4. A connected graph with exactly n − 1 edges, where n is the number
       of vertices.

    5. A graph with exactly one path between any two distinct vertices,
       where a path is a sequence of distinct vertices where each is connected
       to the next by an edge.

    When considering the asymptotic complexity it is often useful to cate-
gorize graphs as dense or sparse. Dense graphs have a lot of edges compared
to the number of vertices. Writing n = |V | for the number of vertices (which
will be our notation in the rest of the lecture) know there can be at most
n ∗ (n − 1)/2: every node is connected to any other node (n ∗ (n − 1)), but in
an undirected way (n ∗ (n − 1)/2). If we write e for the number of edges, we
have e = O(n2 ). By comparison, a tree is sparse because e = n − 1 = O(n).


3    Computing a Spanning Tree
There are many algorithms to compute a spanning tree for a connected
graph. The first is an example of a vertex-centric algorithm.

    1. Pick an arbitrary node and mark it as being in the tree.

    2. Repeat until all nodes are marked as in the tree:

        (a) Pick an arbitrary node u in the tree with an edge e to a node w
            not in the tree. Add e to the spanning tree and mark w as in the
            tree.

We iterate n − 1 times in Step 2, because there are n − 1 vertices that have to
be added to the tree. The efficiency of the algorithm is determined by how
efficiently we can find a qualifying w.
    The second algorithm is edge-centric.

    1. Start with the collection of singleton trees, each with exactly one node.

    2. As long as we have more than one tree, connect two trees together
       with an edge in the graph.



L ECTURE N OTES                                                   A PRIL 26, 2011
Spanning Trees                                                          L26.4


This second algorithm also performs n steps, because it has to add n − 1
edges to the trees until we have a spanning tree. Its efficiency is determined
by how quickly we can tell if an edge would connect two trees or would
connect two nodes already in the same tree, a question we come back to in
the next lecture.
    Let’s try this algorithm on our first graph, considering edges in the
listed order: (AB, BC, CD, AE, BE, CE).


     A
           D
    A
           D
 A
            D
 A
           D

            E
                 E
              E
               E



     B
           C
    B
           C
 B
           C
 B
            C




     A
            D
 A
             D

             E
               E



     B
            C
   B
          C




The first graph is the given graph, the completley disconnected graph is the
starting point for this algorithm. At the bottom right we have computed the
spanning tree, which we know because we have added n − 1 = 4 edges. If
we tried to continue, the next edge BE could not be added because it does
not connect two trees, and neither can CE. The spanning tree is complete.


4   Creating a Random Maze
We can use the algorithm to compute a spanning tree for creating a random
maze. We start with the graph where the vertices are the cells and the
edges represent the neighbors we can move to in the maze. In the graph,
all potential neighbors are connected. A spanning tree will be defined by a
subset of the edges in which all cells in the maze are still connected by some
(unique) path. Because a spanning tree connects all cells, we can arbitrarily
decide on the starting point and end point after we have computed it.


L ECTURE N OTES                                                A PRIL 26, 2011
Spanning Trees                                                                    L26.5


    How would we ensure that the maze is random? The idea is to gener-
ate a random permutation (see Exercise 1) of the edges and then consider
the edges in the fixed order. Each edge is either added (if it connects two
disconnected parts of the maze) or not (if the two vertices are already con-
nected).


5   Minimum Weight Spanning Trees
In many applications of graphs, there is some measure associated with the
edges. For example, when the vertices are locations then the edge weights
could be distances. We might then be interested in not any spanning tree,
but one whose total edge weight is minimal among all the possible span-
ning trees, a so-called minimum weight spanning tree (MST). An MST is not
necessarily unique. For example, all the edge weights could be identical in
which case any spanning tree will be minimal.
    We annotate the edges in our running example with edge weights as
shown on the left below. On the right is the minimum weight spanning
tree, which has weight 9.



                   A
                      D
   A
                   D

                          2
                         2

                                E
                        E

                     3
                   3
                        3

                          2
         2
              2
        2

                   B
                      C
   B
                   C

                               3



    Before we develop a refinement of our edge-centric algorithm for span-
ning trees to take edge weights into account, we discuss a basic property it
is based on.

Cycle Property. Let C ⊆ E be a cycle, and e be an edge of maximal weight
     in C. The e does not need to be in an MST.

    How do we convince ourselves of this property? Assume we have a
spanning tree, and edge e from the cycle property connects vertices u and
w. If e is not in the spanning tree, then, indeed, we don’t need it. If e is in
the spanning tree, we will construct another MST without e. Edge e splits

L ECTURE N OTES                                                          A PRIL 26, 2011
Spanning Trees                                                         L26.6


the spanning tree into two subtrees. There must be another edge e from
C connecting the two subtrees. Removing e and adding e instead yields
another spanning tree, and one which does not contain e. It has equal or
lower weight to the first MST, since e must have less or equal weight than
e.
    The cycle property is the basis for Kruskal’s algorithm.

  1. Sort all edges in increasing weight order.

  2. Consider the edges in order. If the edge does not create a cycle, add
     it to the spanning tree. Otherwise discard it. Stop, when n − 1 edges
     have been added, because then we must have spanning tree.

Why does this create a minimum-weight spanning tree? It is a straightfor-
ward application of the cycle property (see Exercise 2).
    Sorting the edges will take O(e ∗ log(e)) steps with most appropriate
sorting algorithms. The complexity of the second part of the algorithm
depends on how efficiently we can check if adding an edge will create a
cycle or not. As we will see in Lecture 27, this can be O(n ∗ log(n)) or even
more efficient if we use a so-called union-find data structure.
    Illustrating the algorithm on our example



                   A
                     D

                         2

                               E

                    3
                   3

                         2
         2

                   B
                     C

                              3



we first sort the edges. There is some ambiguity—say we obtain the follow-
ing list
                                   AE 2
                                   BE 2
                                   CE 2
                                   BC 3
                                   CD 3
                                   AB 3



L ECTURE N OTES                                               A PRIL 26, 2011
Spanning Trees                                                        L26.7


We now add the edges in order, making sure we do not create a cycle. After
AE, BE, CE, we have



                  A
                     D

                         2

                              E

                        2
         2

                  B
                     C




At this point we consider BC. However, this edge would create a cycle
BCE since it connects two vertices in the same tree instead of two differ-
ent trees. We therefore do not add it to the spanning tree. Next we consider
CD, which does connect two trees. At this point we have a minimum span-
ning tree



                  A
                     D

                         2

                              E

                                        3

                        2
         2

                  B
                     C




We do not consider the last edge, AB, because we have already added n −
1 = 4 edges.
   In the next lecture we will analyze the problem of incrementally adding
edges to a tree in a way that allows us to quickly determine if an edge
would create a cycle.




L ECTURE N OTES                                              A PRIL 26, 2011
Spanning Trees                                                               L26.8


Exercises
Exercise 1 Write a function to generate a random permutation of a given array,
using a random number generator with the interface in the standard rand library.
What is the asymptotic complexity of your function?

Exercise 2 Prove that the cycle property implies the correctness of Kruskal’s algo-
rithm.




L ECTURE N OTES                                                    A PRIL 26, 2011

More Related Content

What's hot

Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
Steven Duplij (Stepan Douplii)
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug designReihaneh Safavi
 
Bd32360363
Bd32360363Bd32360363
Bd32360363
IJERA Editor
 
Graph theory in network system
Graph theory in network systemGraph theory in network system
Graph theory in network system
Manikanta satyala
 
Higher-order graph clustering at AMS Spring Western Sectional
Higher-order graph clustering at AMS Spring Western SectionalHigher-order graph clustering at AMS Spring Western Sectional
Higher-order graph clustering at AMS Spring Western Sectional
Austin Benson
 
Graph theory and life
Graph theory and lifeGraph theory and life
Graph theory and life
Milan Joshi
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
Premsankar Chakkingal
 
A Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
A Fixed Point Theorem Using Common Property (E. A.) In PM SpacesA Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
A Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
inventionjournals
 
Algorithm for Edge Antimagic Labeling for Specific Classes of Graphs
Algorithm for Edge Antimagic Labeling for Specific Classes of GraphsAlgorithm for Edge Antimagic Labeling for Specific Classes of Graphs
Algorithm for Edge Antimagic Labeling for Specific Classes of Graphs
CSCJournals
 
Lecture 9 dim & rank - 4-5 & 4-6
Lecture 9   dim & rank -  4-5 & 4-6Lecture 9   dim & rank -  4-5 & 4-6
Lecture 9 dim & rank - 4-5 & 4-6
njit-ronbrown
 
Some special artex spaces over bi monoids
Some special artex spaces over bi monoidsSome special artex spaces over bi monoids
Some special artex spaces over bi monoidsAlexander Decker
 
Rigidity And Tensegrity By Connelly
Rigidity And Tensegrity By ConnellyRigidity And Tensegrity By Connelly
Rigidity And Tensegrity By Connelly
Tensegrity Wiki
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Graph theory and its applications
Graph theory and its applicationsGraph theory and its applications
Graph theory and its applications
Manikanta satyala
 
Common Fixed Point Theorem in Menger Space through weak Compatibility
Common Fixed Point Theorem in Menger Space through weak CompatibilityCommon Fixed Point Theorem in Menger Space through weak Compatibility
Common Fixed Point Theorem in Menger Space through weak Compatibility
inventionjournals
 
Higher-order clustering coefficients
Higher-order clustering coefficientsHigher-order clustering coefficients
Higher-order clustering coefficients
Austin Benson
 
Common fixed point theorems using faintly compatible
Common fixed point theorems using faintly compatibleCommon fixed point theorems using faintly compatible
Common fixed point theorems using faintly compatible
Alexander Decker
 
One modulo n gracefulness of
One modulo n gracefulness ofOne modulo n gracefulness of
One modulo n gracefulness of
graphhoc
 

What's hot (20)

Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
Conformal symmetry transformations and nonlinear Maxwell equations, G.A.Goldi...
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug design
 
Bd32360363
Bd32360363Bd32360363
Bd32360363
 
Graph theory in network system
Graph theory in network systemGraph theory in network system
Graph theory in network system
 
Higher-order graph clustering at AMS Spring Western Sectional
Higher-order graph clustering at AMS Spring Western SectionalHigher-order graph clustering at AMS Spring Western Sectional
Higher-order graph clustering at AMS Spring Western Sectional
 
Graph theory and life
Graph theory and lifeGraph theory and life
Graph theory and life
 
Lecture3
Lecture3Lecture3
Lecture3
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Connectivity of graph
Connectivity of graphConnectivity of graph
Connectivity of graph
 
A Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
A Fixed Point Theorem Using Common Property (E. A.) In PM SpacesA Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
A Fixed Point Theorem Using Common Property (E. A.) In PM Spaces
 
Algorithm for Edge Antimagic Labeling for Specific Classes of Graphs
Algorithm for Edge Antimagic Labeling for Specific Classes of GraphsAlgorithm for Edge Antimagic Labeling for Specific Classes of Graphs
Algorithm for Edge Antimagic Labeling for Specific Classes of Graphs
 
Lecture 9 dim & rank - 4-5 & 4-6
Lecture 9   dim & rank -  4-5 & 4-6Lecture 9   dim & rank -  4-5 & 4-6
Lecture 9 dim & rank - 4-5 & 4-6
 
Some special artex spaces over bi monoids
Some special artex spaces over bi monoidsSome special artex spaces over bi monoids
Some special artex spaces over bi monoids
 
Rigidity And Tensegrity By Connelly
Rigidity And Tensegrity By ConnellyRigidity And Tensegrity By Connelly
Rigidity And Tensegrity By Connelly
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Graph theory and its applications
Graph theory and its applicationsGraph theory and its applications
Graph theory and its applications
 
Common Fixed Point Theorem in Menger Space through weak Compatibility
Common Fixed Point Theorem in Menger Space through weak CompatibilityCommon Fixed Point Theorem in Menger Space through weak Compatibility
Common Fixed Point Theorem in Menger Space through weak Compatibility
 
Higher-order clustering coefficients
Higher-order clustering coefficientsHigher-order clustering coefficients
Higher-order clustering coefficients
 
Common fixed point theorems using faintly compatible
Common fixed point theorems using faintly compatibleCommon fixed point theorems using faintly compatible
Common fixed point theorems using faintly compatible
 
One modulo n gracefulness of
One modulo n gracefulness ofOne modulo n gracefulness of
One modulo n gracefulness of
 

Viewers also liked

Pt Duta Business School
Pt Duta Business SchoolPt Duta Business School
Pt Duta Business Schoolmuchnigroups
 
6701732721 f
6701732721 f6701732721 f
6701732721 f
Al Baha University
 
Sin With Sebastian introduction
Sin With Sebastian introductionSin With Sebastian introduction
Sin With Sebastian introduction
ClaudiaVonSinner
 
اللوحة التفاعلية
اللوحة التفاعليةاللوحة التفاعلية
اللوحة التفاعليةre-sweet
 
1960's history project
1960's history project1960's history project
1960's history projectBrandi28
 
Senior project speech
Senior project speechSenior project speech
Senior project speechlukas115
 
꾸뻬 씨의 행복여행
꾸뻬 씨의 행복여행꾸뻬 씨의 행복여행
꾸뻬 씨의 행복여행
Ji Young Kim
 
Power of Email Marketing (NADA 2010) Peter Martin
Power of Email Marketing (NADA 2010) Peter MartinPower of Email Marketing (NADA 2010) Peter Martin
Power of Email Marketing (NADA 2010) Peter Martin
Sean Bradley
 
The best of SEO toturial
The best of SEO toturialThe best of SEO toturial
The best of SEO toturial
boyfvn
 
Inv pres q12014 final
Inv pres q12014   finalInv pres q12014   final
Inv pres q12014 finalCNOServices
 
انجيل يوحنا
انجيل يوحناانجيل يوحنا
انجيل يوحنا
Ibrahimia Church Ftriends
 
Pollution hereglegdhuun 9 р анги сарантуяа
Pollution  hereglegdhuun 9 р анги сарантуяаPollution  hereglegdhuun 9 р анги сарантуяа
Pollution hereglegdhuun 9 р анги сарантуяа
sarantuya choimzon
 
Khemjira portfolio 2558
Khemjira portfolio 2558Khemjira portfolio 2558
Khemjira portfolio 2558
Khemjira_P
 
2011 0125 platt uscc_model_state_policies
2011 0125 platt uscc_model_state_policies2011 0125 platt uscc_model_state_policies
2011 0125 platt uscc_model_state_policiesspickell
 
Copyrights a.hawkins1
Copyrights a.hawkins1Copyrights a.hawkins1
Copyrights a.hawkins1hawkinshhs
 
Bbbs maksud & ciri ciri sej baca
Bbbs maksud & ciri ciri sej bacaBbbs maksud & ciri ciri sej baca
Bbbs maksud & ciri ciri sej bacasamshitha
 
Scott Pechstein – Why buying 3rd party is still the most efficient advertisement
Scott Pechstein – Why buying 3rd party is still the most efficient advertisementScott Pechstein – Why buying 3rd party is still the most efficient advertisement
Scott Pechstein – Why buying 3rd party is still the most efficient advertisement
Sean Bradley
 
How To Deal With Toddler Tantrums
How To Deal With Toddler TantrumsHow To Deal With Toddler Tantrums
How To Deal With Toddler Tantrums
bridgetbarnes102
 

Viewers also liked (20)

Pt Duta Business School
Pt Duta Business SchoolPt Duta Business School
Pt Duta Business School
 
6701732721 f
6701732721 f6701732721 f
6701732721 f
 
Sin With Sebastian introduction
Sin With Sebastian introductionSin With Sebastian introduction
Sin With Sebastian introduction
 
اللوحة التفاعلية
اللوحة التفاعليةاللوحة التفاعلية
اللوحة التفاعلية
 
1960's history project
1960's history project1960's history project
1960's history project
 
Autoloans
AutoloansAutoloans
Autoloans
 
Senior project speech
Senior project speechSenior project speech
Senior project speech
 
꾸뻬 씨의 행복여행
꾸뻬 씨의 행복여행꾸뻬 씨의 행복여행
꾸뻬 씨의 행복여행
 
Power of Email Marketing (NADA 2010) Peter Martin
Power of Email Marketing (NADA 2010) Peter MartinPower of Email Marketing (NADA 2010) Peter Martin
Power of Email Marketing (NADA 2010) Peter Martin
 
The best of SEO toturial
The best of SEO toturialThe best of SEO toturial
The best of SEO toturial
 
Inv pres q12014 final
Inv pres q12014   finalInv pres q12014   final
Inv pres q12014 final
 
انجيل يوحنا
انجيل يوحناانجيل يوحنا
انجيل يوحنا
 
Pollution hereglegdhuun 9 р анги сарантуяа
Pollution  hereglegdhuun 9 р анги сарантуяаPollution  hereglegdhuun 9 р анги сарантуяа
Pollution hereglegdhuun 9 р анги сарантуяа
 
Khemjira portfolio 2558
Khemjira portfolio 2558Khemjira portfolio 2558
Khemjira portfolio 2558
 
Canribas ci
Canribas ciCanribas ci
Canribas ci
 
2011 0125 platt uscc_model_state_policies
2011 0125 platt uscc_model_state_policies2011 0125 platt uscc_model_state_policies
2011 0125 platt uscc_model_state_policies
 
Copyrights a.hawkins1
Copyrights a.hawkins1Copyrights a.hawkins1
Copyrights a.hawkins1
 
Bbbs maksud & ciri ciri sej baca
Bbbs maksud & ciri ciri sej bacaBbbs maksud & ciri ciri sej baca
Bbbs maksud & ciri ciri sej baca
 
Scott Pechstein – Why buying 3rd party is still the most efficient advertisement
Scott Pechstein – Why buying 3rd party is still the most efficient advertisementScott Pechstein – Why buying 3rd party is still the most efficient advertisement
Scott Pechstein – Why buying 3rd party is still the most efficient advertisement
 
How To Deal With Toddler Tantrums
How To Deal With Toddler TantrumsHow To Deal With Toddler Tantrums
How To Deal With Toddler Tantrums
 

Similar to 26 spanning

PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
SUTAPAMUKHERJEE12
 
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 strctureszukun
 
Graph clustering
Graph clusteringGraph clustering
Graph clustering
ssusered887b
 
Graph theory ppt.pptx
Graph theory ppt.pptxGraph theory ppt.pptx
Graph theory ppt.pptx
saranyajey
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
Jyoshna Cec Cse Staf bejjam
 
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
nabati
 
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
cscpconf
 
Tree, function and graph
Tree, function and graphTree, function and graph
Tree, function and graph
University of Potsdam
 
Spectral Clustering Report
Spectral Clustering ReportSpectral Clustering Report
Spectral Clustering ReportMiaolan Xie
 
Network Topology
Network TopologyNetwork Topology
Network Topology
Harsh Soni
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
infanciaj
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel
 
Social network analysis study ch3 4.2-120108
Social network analysis study ch3 4.2-120108Social network analysis study ch3 4.2-120108
Social network analysis study ch3 4.2-120108SungKyu Shaun Park
 
Line
LineLine
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
alisha230390
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king779879
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
 
Graph in data structures
Graph in data structuresGraph in data structures
Graph in data structures
AhsanRazaKolachi
 
1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
 1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf 1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
AHUJAMUSICCOLLECTION
 

Similar to 26 spanning (20)

PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
 
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 clustering
Graph clusteringGraph clustering
Graph clustering
 
Graph theory ppt.pptx
Graph theory ppt.pptxGraph theory ppt.pptx
Graph theory ppt.pptx
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
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
 
logic.pptx
logic.pptxlogic.pptx
logic.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
 
Tree, function and graph
Tree, function and graphTree, function and graph
Tree, function and graph
 
Spectral Clustering Report
Spectral Clustering ReportSpectral Clustering Report
Spectral Clustering Report
 
Network Topology
Network TopologyNetwork Topology
Network Topology
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Social network analysis study ch3 4.2-120108
Social network analysis study ch3 4.2-120108Social network analysis study ch3 4.2-120108
Social network analysis study ch3 4.2-120108
 
Line
LineLine
Line
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Graph in data structures
Graph in data structuresGraph in data structures
Graph in data structures
 
1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
 1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf 1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
1. Trees Recall that a tree is a connected acyclic graph. Note that t.pdf
 

Recently uploaded

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 

26 spanning

  • 1. Lecture Notes on Spanning Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 26 April 26, 2011 1 Introduction In this lecture we introduce graphs. Graphs provide a uniform model for many structures, for example, maps with distances or Facebook relation- ships. Algorithms on graphs are therefore important to many applications. They will be a central subject in the algorithms courses later in the curricu- lum; here we only provide a very small sample of graph algorithms. 2 Spanning Trees We start with undirected graphs which consist of a set V of vertices (also called nodes) and a set E of edges, each connecting two different vertices. A graph is connected if we can reach any vertex from any other vertex by following edges in either direction. In a directed graph edges provide a connection from one node to another, but not necessarily in the opposite direction. More mathematically, we say that the edge relation between ver- tices is symmetric for undirected graphs. In this lecture we only discuss undirected graphs, although directed graphs also play an important role in many applications. The following is a simple example of a connected, undirected graph L ECTURE N OTES A PRIL 26, 2011
  • 2. Spanning Trees L26.2 with 5 vertices (A, B, C, D, E) and 6 edges (AB, BC, CD, AE, BE, CE). A
 D
 E
 B
 C
 In this lecture we are particularly interested in the problem of computing a spanning tree for a connected graph. What is a tree here? They are a bit different than the binary search trees we considered early. One simple definition is that a tree is a connected graph with no cycles, where a cycle let’s you go from a node to itself without repeating an edge. A spanning tree for a connected graph G is a tree containing all the vertices of G. Below are two examples of spanning trees for our original example graph. A
 D
 A
 D
 E
 E
 B
 C
 B
 C
 When dealing with a new kind of data structure, it is a good strategy to try to think of as many different characterization as we can. This is some- what similar to the problem of coming up with good representations of the data; different ones may be appropriate for different purposes. Here are some alternative characterizations the class came up with: 1. Connected graph with no cycle (original). 2. Connected graph where no two neighbors are otherwise connected. Neighbors are vertices connected directly by an edge, otherwise con- nected means connected without the connecting edge. L ECTURE N OTES A PRIL 26, 2011
  • 3. Spanning Trees L26.3 3. Two trees connected by a single edge. This is a recursive characteriza- tion. The based case is a single node, with the empty tree (no vertices) as a possible special case. 4. A connected graph with exactly n − 1 edges, where n is the number of vertices. 5. A graph with exactly one path between any two distinct vertices, where a path is a sequence of distinct vertices where each is connected to the next by an edge. When considering the asymptotic complexity it is often useful to cate- gorize graphs as dense or sparse. Dense graphs have a lot of edges compared to the number of vertices. Writing n = |V | for the number of vertices (which will be our notation in the rest of the lecture) know there can be at most n ∗ (n − 1)/2: every node is connected to any other node (n ∗ (n − 1)), but in an undirected way (n ∗ (n − 1)/2). If we write e for the number of edges, we have e = O(n2 ). By comparison, a tree is sparse because e = n − 1 = O(n). 3 Computing a Spanning Tree There are many algorithms to compute a spanning tree for a connected graph. The first is an example of a vertex-centric algorithm. 1. Pick an arbitrary node and mark it as being in the tree. 2. Repeat until all nodes are marked as in the tree: (a) Pick an arbitrary node u in the tree with an edge e to a node w not in the tree. Add e to the spanning tree and mark w as in the tree. We iterate n − 1 times in Step 2, because there are n − 1 vertices that have to be added to the tree. The efficiency of the algorithm is determined by how efficiently we can find a qualifying w. The second algorithm is edge-centric. 1. Start with the collection of singleton trees, each with exactly one node. 2. As long as we have more than one tree, connect two trees together with an edge in the graph. L ECTURE N OTES A PRIL 26, 2011
  • 4. Spanning Trees L26.4 This second algorithm also performs n steps, because it has to add n − 1 edges to the trees until we have a spanning tree. Its efficiency is determined by how quickly we can tell if an edge would connect two trees or would connect two nodes already in the same tree, a question we come back to in the next lecture. Let’s try this algorithm on our first graph, considering edges in the listed order: (AB, BC, CD, AE, BE, CE). A
 D
 A
 D
 A
 D
 A
 D
 E
 E
 E
 E
 B
 C
 B
 C
 B
 C
 B
 C
 A
 D
 A
 D
 E
 E
 B
 C
 B
 C
 The first graph is the given graph, the completley disconnected graph is the starting point for this algorithm. At the bottom right we have computed the spanning tree, which we know because we have added n − 1 = 4 edges. If we tried to continue, the next edge BE could not be added because it does not connect two trees, and neither can CE. The spanning tree is complete. 4 Creating a Random Maze We can use the algorithm to compute a spanning tree for creating a random maze. We start with the graph where the vertices are the cells and the edges represent the neighbors we can move to in the maze. In the graph, all potential neighbors are connected. A spanning tree will be defined by a subset of the edges in which all cells in the maze are still connected by some (unique) path. Because a spanning tree connects all cells, we can arbitrarily decide on the starting point and end point after we have computed it. L ECTURE N OTES A PRIL 26, 2011
  • 5. Spanning Trees L26.5 How would we ensure that the maze is random? The idea is to gener- ate a random permutation (see Exercise 1) of the edges and then consider the edges in the fixed order. Each edge is either added (if it connects two disconnected parts of the maze) or not (if the two vertices are already con- nected). 5 Minimum Weight Spanning Trees In many applications of graphs, there is some measure associated with the edges. For example, when the vertices are locations then the edge weights could be distances. We might then be interested in not any spanning tree, but one whose total edge weight is minimal among all the possible span- ning trees, a so-called minimum weight spanning tree (MST). An MST is not necessarily unique. For example, all the edge weights could be identical in which case any spanning tree will be minimal. We annotate the edges in our running example with edge weights as shown on the left below. On the right is the minimum weight spanning tree, which has weight 9. A
 D
 A
 D
 2
 2
 E
 E
 3
 3
 3
 2
 2
 2
 2
 B
 C
 B
 C
 3
 Before we develop a refinement of our edge-centric algorithm for span- ning trees to take edge weights into account, we discuss a basic property it is based on. Cycle Property. Let C ⊆ E be a cycle, and e be an edge of maximal weight in C. The e does not need to be in an MST. How do we convince ourselves of this property? Assume we have a spanning tree, and edge e from the cycle property connects vertices u and w. If e is not in the spanning tree, then, indeed, we don’t need it. If e is in the spanning tree, we will construct another MST without e. Edge e splits L ECTURE N OTES A PRIL 26, 2011
  • 6. Spanning Trees L26.6 the spanning tree into two subtrees. There must be another edge e from C connecting the two subtrees. Removing e and adding e instead yields another spanning tree, and one which does not contain e. It has equal or lower weight to the first MST, since e must have less or equal weight than e. The cycle property is the basis for Kruskal’s algorithm. 1. Sort all edges in increasing weight order. 2. Consider the edges in order. If the edge does not create a cycle, add it to the spanning tree. Otherwise discard it. Stop, when n − 1 edges have been added, because then we must have spanning tree. Why does this create a minimum-weight spanning tree? It is a straightfor- ward application of the cycle property (see Exercise 2). Sorting the edges will take O(e ∗ log(e)) steps with most appropriate sorting algorithms. The complexity of the second part of the algorithm depends on how efficiently we can check if adding an edge will create a cycle or not. As we will see in Lecture 27, this can be O(n ∗ log(n)) or even more efficient if we use a so-called union-find data structure. Illustrating the algorithm on our example A
 D
 2
 E
 3
 3
 2
 2
 B
 C
 3
 we first sort the edges. There is some ambiguity—say we obtain the follow- ing list AE 2 BE 2 CE 2 BC 3 CD 3 AB 3 L ECTURE N OTES A PRIL 26, 2011
  • 7. Spanning Trees L26.7 We now add the edges in order, making sure we do not create a cycle. After AE, BE, CE, we have A
 D
 2
 E
 2
 2
 B
 C
 At this point we consider BC. However, this edge would create a cycle BCE since it connects two vertices in the same tree instead of two differ- ent trees. We therefore do not add it to the spanning tree. Next we consider CD, which does connect two trees. At this point we have a minimum span- ning tree A
 D
 2
 E
 3
 2
 2
 B
 C
 We do not consider the last edge, AB, because we have already added n − 1 = 4 edges. In the next lecture we will analyze the problem of incrementally adding edges to a tree in a way that allows us to quickly determine if an edge would create a cycle. L ECTURE N OTES A PRIL 26, 2011
  • 8. Spanning Trees L26.8 Exercises Exercise 1 Write a function to generate a random permutation of a given array, using a random number generator with the interface in the standard rand library. What is the asymptotic complexity of your function? Exercise 2 Prove that the cycle property implies the correctness of Kruskal’s algo- rithm. L ECTURE N OTES A PRIL 26, 2011