SlideShare une entreprise Scribd logo
1  sur  8
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 
Graph theory in network system
Graph theory in network systemGraph theory in network system
Graph theory in network systemManikanta 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 SectionalAustin Benson
 
Graph theory and life
Graph theory and lifeGraph theory and life
Graph theory and lifeMilan Joshi
 
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 Spacesinventionjournals
 
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 GraphsCSCJournals
 
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-6njit-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 ConnellyTensegrity 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 applicationsManikanta 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 Compatibilityinventionjournals
 
Higher-order clustering coefficients
Higher-order clustering coefficientsHigher-order clustering coefficients
Higher-order clustering coefficientsAustin 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 compatibleAlexander Decker
 
One modulo n gracefulness of
One modulo n gracefulness ofOne modulo n gracefulness of
One modulo n gracefulness ofgraphhoc
 

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

En vedette

Pt Duta Business School
Pt Duta Business SchoolPt Duta Business School
Pt Duta Business Schoolmuchnigroups
 
Sin With Sebastian introduction
Sin With Sebastian introductionSin With Sebastian introduction
Sin With Sebastian introductionClaudiaVonSinner
 
اللوحة التفاعلية
اللوحة التفاعليةاللوحة التفاعلية
اللوحة التفاعلية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 MartinSean Bradley
 
The best of SEO toturial
The best of SEO toturialThe best of SEO toturial
The best of SEO toturialboyfvn
 
Inv pres q12014 final
Inv pres q12014   finalInv pres q12014   final
Inv pres q12014 finalCNOServices
 
Pollution hereglegdhuun 9 р анги сарантуяа
Pollution  hereglegdhuun 9 р анги сарантуяаPollution  hereglegdhuun 9 р анги сарантуяа
Pollution hereglegdhuun 9 р анги сарантуяаsarantuya choimzon
 
Khemjira portfolio 2558
Khemjira portfolio 2558Khemjira portfolio 2558
Khemjira portfolio 2558Khemjira_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 advertisementSean Bradley
 
How To Deal With Toddler Tantrums
How To Deal With Toddler TantrumsHow To Deal With Toddler Tantrums
How To Deal With Toddler Tantrumsbridgetbarnes102
 

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

Similaire à 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 theory ppt.pptx
Graph theory ppt.pptxGraph theory ppt.pptx
Graph theory ppt.pptxsaranyajey
 
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 nabatinabati
 
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 CLASSIFICATIONcscpconf
 
Spectral Clustering Report
Spectral Clustering ReportSpectral Clustering Report
Spectral Clustering ReportMiaolan Xie
 
Network Topology
Network TopologyNetwork Topology
Network TopologyHarsh Soni
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2infanciaj
 
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
 
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 osalisha230390
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
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.pdfAHUJAMUSICCOLLECTION
 

Similaire à 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
 

Dernier

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
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.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
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 13Steve Thomason
 
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 ImpactPECB
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
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 communicationnomboosow
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Dernier (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
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
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
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
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

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