SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
Graph Algorithms




        Mathematical Structures
         for Computer Science
                Chapter 6	





Copyright © 2006 W.H. Freeman & Co.     MSCS Slides   Graph Algorithms
Shortest Path Problem

     ●        Assume that we have a simple, weighted, connected
              graph, where the weights are positive. Then a path
              exists between any two nodes x and y.	

     ●        How do we find a path with minimum weight?	

     ●        For example, cities connected by roads, with the
              weight being the distance between them.	

     ●        The shortest-path algorithm is known as Dijkstra’s
              algorithm.	





Section 6.3                      Shortest Path and Minimal Spanning Tree   1
Shortest-Path Algorithm

     ●        To compute the shortest path from x to y using
              Dijkstra’s algorithm, we build a set (called IN ) that
              initially contains only x but grows as the algorithm
              proceeds.	

     ●        IN contains every node whose shortest path from x,
              using only nodes in IN, has so far been determined.	

     ●        For every node z outside IN, we keep track of the
              shortest distance d[z] from x to that node, using a path
              whose only non-IN node is z.	

     ●        We also keep track of the node adjacent to z on this
              path, s[z].	




Section 6.3                       Shortest Path and Minimal Spanning Tree   2
Shortest-Path Algorithm

     ●        Pick the non-IN node p with the smallest distance d.	

     ●        Add p to IN, then recompute d for all the remaining
              non-IN nodes, because there may be a shorter path
              from x going through p than there was before p
              belonged to IN.	

     ●        If there is a shorter path, update s[z] so that p is now
              shown to be the node adjacent to z on the current
              shortest path.	

     ●        As soon as y is moved into IN, IN stops growing. The
              current value of d[y] is the distance for the shortest
              path, and its nodes are found by looking at y, s[y], s[s
              [y]], and so forth, until x is reached.	


Section 6.3                        Shortest Path and Minimal Spanning Tree   3
Shortest-Path Algorithm
     ●        ALGORITHM ShortestPath
              ShortestPath (n × n matrix A; nodes x, y)
              //Dijkstra’s algorithm. A is a modified adjacency matrix for a
              //simple, connected graph with positive weights; x and y are
              //nodes in the graph; writes out nodes in the shortest path from x
              // to y, and the distance for that path
                 Local variables:
                 set of nodes IN //set of nodes whose shortest path from x
                 //is known
                 nodes z, p //temporary nodes
                 array of integers d //for each node, the distance from x using
                 //nodes in IN
                 array of nodes s //for each node, the previous node in the
                 //shortest path
                 integer OldDistance //distance to compare against
                 //initialize set IN and arrays d and s
                 IN = {x}
                 d[x] = 0	


Section 6.3                            Shortest Path and Minimal Spanning Tree        4
Shortest-Path Algorithm

          	

for all nodes z not in IN do
                  d[z] = A[x, z]
                  s [z] = x
             end for//process nodes into IN
             while y not in IN do
               //add minimum-distance node not in IN
               p = node z not in IN with minimum d[z]
               IN = IN ∪ {p}
               //recompute d for non-IN nodes, adjust s if necessary
               for all nodes z not in IN do
                  OldDistance = d[z]
                  d[z] = min(d[z], d[ p] + A[ p, z])
                  if d[z] != OldDistance then
                     s [z] = p
                  end if
               end for
             end while	



Section 6.3                         Shortest Path and Minimal Spanning Tree   5
Shortest-Path Algorithm

       	

//write out path nodes
          write(“In reverse order, the path is”)
          write (y)
          z = y
          repeat
             write (s [z])
             z = s [z]
          until z = x
          // write out path distance
          write(“The path distance is,” d[y])	

     end ShortestPath	





Section 6.3                         Shortest Path and Minimal Spanning Tree   6
Shortest-Path Algorithm Example

     ●        Trace the algorithm using the following graph and
              adjacency matrix:	





     ●        At the end of the initialization phase:	




Section 6.3                         Shortest Path and Minimal Spanning Tree   7
Shortest-Path Algorithm Example

     ●        The circled nodes are those in set IN. heavy lines show the
              current shortest paths, and the d-value for each node is
              written along with the node label.	





Section 6.3                        Shortest Path and Minimal Spanning Tree   8
Shortest-Path Algorithm Example

     ●        We now enter the while loop and search through the d-values
              for the node of minimum distance that is not in IN. Node 1 is
              found, with d[1] = 3.	

     ●        Node 1 is added to IN. We recompute all the d-values for the
              remaining nodes, 2, 3, 4, and y.	

          	

      	

     	

p = 1	

          	

      	

     	

IN = {x,1}	

          	

      	

     	

d[2] = min(8, 3 + A[1, 2]) = min(8, ∞) = 8	

          	

      	

     	

d[3] = min(4, 3 + A[1, 3]) = min(4, 9) = 4	

          	

      	

     	

d[4] = min(∞, 3 + A[1, 4]) = min(∞, ∞) = ∞	

          	

      	

     	

d[y] = min(10, 3 + A[1, y]) = min(10, ∞) = 10	

     ●        This process is repeated until y is added to IN.	





Section 6.3                         Shortest Path and Minimal Spanning Tree      9
Shortest-Path Algorithm Example

     ●        The second pass through the while loop:	

              ■    p = 3 (3 has the smallest d-value, namely 4, of 2, 3, 4, or y)	

              ■    IN = {x, 1, 3}	

              ■    d[2] = min(8, 4 + A[3, 2]) = min(8, 4 + ∞) = 8	

              ■    d[4] = min(∞, 4 + A[3, 4]) = min(∞, 4 + 1) = 5 (a change, so
                   update s[4] to 3)	

              ■    d[y] = min(10, 4 + A[3, y]) = min(10, 4 + 3) = 7 (a change, so
                   update s[y] to 3)	





Section 6.3                                Shortest Path and Minimal Spanning Tree     10
Shortest-Path Algorithm Example

     ●        The third pass through the while loop:	

              ■    p = 4 (d-value 5)	

              ■    IN = {x, 1, 3, 4}	

              ■    d[2] = min(8, 5 + 7) = 8	

              ■    d[y] = min(7, 5 + 1) = 6 (a change, update s[y])	





Section 6.3                           Shortest Path and Minimal Spanning Tree   11
Shortest-Path Algorithm Example

     ●        The third pass through the while loop:	

              ■    p = y	

              ■    IN = {x, 1, 3, 4, y}	

              ■    d[2] = min(8, 6 ) = 8	





     ●        y is now part of IN, so the while loop terminates. The
              (reversed) path goes through y, s[y]= 4, s[4]= 3, and s
              [3] = x.	


Section 6.3                            Shortest Path and Minimal Spanning Tree   12
Shortest-Path Algorithm Analysis

     ●        ShortestPath is a greedy algorithm - it does what
              seems best based on its limited immediate knowledge.	

     ●        The for loop requires Θ(n) operations.	

     ●        The while loop takes Θ(n) operations.	

     ●        In the worst case, y is the last node brought into IN,
              and the while loop will be executed n - 1 times.	

     ●        The total number of operations involved in the while
              loop is Θ(n(n - 1)) = Θ(n2 ).	

     ●        Initialization and writing the output together take Θ(n)
              operations.	

     ●        So the algorithm requires Θ(n + n2) = Θ(n2 )
              operations in the worst case.	


Section 6.3                       Shortest Path and Minimal Spanning Tree   13
Minimal Spanning Tree Problem

     ●        DEFINITION: SPANNING TREE 
              A spanning tree for a connected graph is a nonrooted tree
              whose set of nodes coincides with the set of nodes for the
              graph and whose arcs are (some of) the arcs of the graph.	

     ●        A spanning tree connects all the nodes of a graph with no
              excess arcs (no cycles). There are algorithms for
              constructing a minimal spanning tree, a spanning tree
              with minimal weight, for a given simple, weighted,
              connected graph.	

     ●        Prim’s Algorithm proceeds very much like the shortest-
              path algorithm, resulting in a minimal spanning tree.	





Section 6.3                         Shortest Path and Minimal Spanning Tree   14
Prim’s Algorithm

     ●        There is a set IN, which initially contains one arbitrary node.	

     ●        For every node z not in IN, we keep track of the shortest
              distance d[z] between z and any node in IN.	

     ●        We successively add nodes to IN, where the next node added is
              one that is not in IN and whose distance d[z] is minimal.	

     ●        The arc having this minimal distance is then made part of the
              spanning tree.	

     ●        The minimal spanning tree of a graph may not be unique.	

     ●        The algorithm terminates when all nodes of the graph are in IN.	

     ●        The difference between Prim’s and Dijkstra’s algorithm is how
              d[z] (new distances) are calculated.

              Dijkstra’s: d[z] = min(d[z], d[p] + A[ p, z])
              Prim’s:     d[z] = min(d[z], A[ p, z]))	



Section 6.3                            Shortest Path and Minimal Spanning Tree     15

Weitere ähnliche Inhalte

Was ist angesagt?

Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 
(DL hacks輪読) Variational Inference with Rényi Divergence
(DL hacks輪読) Variational Inference with Rényi Divergence(DL hacks輪読) Variational Inference with Rényi Divergence
(DL hacks輪読) Variational Inference with Rényi DivergenceMasahiro Suzuki
 
Modeling biased tracers at the field level
Modeling biased tracers at the field levelModeling biased tracers at the field level
Modeling biased tracers at the field levelMarcel Schmittfull
 
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...Avichai Cohen
 
Fft presentation
Fft presentationFft presentation
Fft presentationilker Şin
 
Kriging and spatial design accelerated by orders of magnitude
Kriging and spatial design accelerated by orders of magnitudeKriging and spatial design accelerated by orders of magnitude
Kriging and spatial design accelerated by orders of magnitudeAlexander Litvinenko
 
Introduction to complexity theory assignment
Introduction to complexity theory assignmentIntroduction to complexity theory assignment
Introduction to complexity theory assignmenttesfahunegn minwuyelet
 
M. Haack - Nernst Branes in Gauged Supergravity
M. Haack - Nernst Branes in Gauged SupergravityM. Haack - Nernst Branes in Gauged Supergravity
M. Haack - Nernst Branes in Gauged SupergravitySEENET-MTP
 
Murpy's Machine Learning:14. Kernel
Murpy's Machine Learning:14. KernelMurpy's Machine Learning:14. Kernel
Murpy's Machine Learning:14. KernelJungkyu Lee
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomeworkGopi Saiteja
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
Tensor Decomposition and its Applications
Tensor Decomposition and its ApplicationsTensor Decomposition and its Applications
Tensor Decomposition and its ApplicationsKeisuke OTAKI
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesAntonis Antonopoulos
 

Was ist angesagt? (19)

Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 
Lecture5
Lecture5Lecture5
Lecture5
 
(DL hacks輪読) Variational Inference with Rényi Divergence
(DL hacks輪読) Variational Inference with Rényi Divergence(DL hacks輪読) Variational Inference with Rényi Divergence
(DL hacks輪読) Variational Inference with Rényi Divergence
 
Modeling biased tracers at the field level
Modeling biased tracers at the field levelModeling biased tracers at the field level
Modeling biased tracers at the field level
 
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
Amirim Project - Threshold Functions in Random Simplicial Complexes - Avichai...
 
Fft presentation
Fft presentationFft presentation
Fft presentation
 
Kriging and spatial design accelerated by orders of magnitude
Kriging and spatial design accelerated by orders of magnitudeKriging and spatial design accelerated by orders of magnitude
Kriging and spatial design accelerated by orders of magnitude
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
Introduction to complexity theory assignment
Introduction to complexity theory assignmentIntroduction to complexity theory assignment
Introduction to complexity theory assignment
 
Dsp lecture vol 2 dft & fft
Dsp lecture vol 2 dft & fftDsp lecture vol 2 dft & fft
Dsp lecture vol 2 dft & fft
 
M. Haack - Nernst Branes in Gauged Supergravity
M. Haack - Nernst Branes in Gauged SupergravityM. Haack - Nernst Branes in Gauged Supergravity
M. Haack - Nernst Branes in Gauged Supergravity
 
Dif fft
Dif fftDif fft
Dif fft
 
Murpy's Machine Learning:14. Kernel
Murpy's Machine Learning:14. KernelMurpy's Machine Learning:14. Kernel
Murpy's Machine Learning:14. Kernel
 
Ee693 questionshomework
Ee693 questionshomeworkEe693 questionshomework
Ee693 questionshomework
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Tensor Decomposition and its Applications
Tensor Decomposition and its ApplicationsTensor Decomposition and its Applications
Tensor Decomposition and its Applications
 
Assignment 2 daa
Assignment 2 daaAssignment 2 daa
Assignment 2 daa
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity Classes
 

Andere mochten auch

M2 Roadshow Mark Curtis Flirtomatic
M2 Roadshow Mark Curtis FlirtomaticM2 Roadshow Mark Curtis Flirtomatic
M2 Roadshow Mark Curtis Flirtomaticmobilesquared Ltd
 
Web Design For The Nondesigner
Web Design For The NondesignerWeb Design For The Nondesigner
Web Design For The NondesignerCharlie Kalech
 
Roadshow Europe Qualcomm - Julian Harris
Roadshow Europe Qualcomm - Julian HarrisRoadshow Europe Qualcomm - Julian Harris
Roadshow Europe Qualcomm - Julian Harrismobilesquared Ltd
 
Connecting standards users_by_using_internet_tools_haim_oren
Connecting standards users_by_using_internet_tools_haim_orenConnecting standards users_by_using_internet_tools_haim_oren
Connecting standards users_by_using_internet_tools_haim_orenThe Oren Group
 

Andere mochten auch (6)

M2 Roadshow Mark Curtis Flirtomatic
M2 Roadshow Mark Curtis FlirtomaticM2 Roadshow Mark Curtis Flirtomatic
M2 Roadshow Mark Curtis Flirtomatic
 
Web Design For The Nondesigner
Web Design For The NondesignerWeb Design For The Nondesigner
Web Design For The Nondesigner
 
Mobile squared issue 9
Mobile squared issue 9Mobile squared issue 9
Mobile squared issue 9
 
Roadshow Europe Qualcomm - Julian Harris
Roadshow Europe Qualcomm - Julian HarrisRoadshow Europe Qualcomm - Julian Harris
Roadshow Europe Qualcomm - Julian Harris
 
Ue pan american-news max-sept-2010
Ue pan american-news max-sept-2010Ue pan american-news max-sept-2010
Ue pan american-news max-sept-2010
 
Connecting standards users_by_using_internet_tools_haim_oren
Connecting standards users_by_using_internet_tools_haim_orenConnecting standards users_by_using_internet_tools_haim_oren
Connecting standards users_by_using_internet_tools_haim_oren
 

Ähnlich wie CPSC125 ch6 sec3

A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpathCarlostheran
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfzefergaming
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersAnimesh Chaturvedi
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treeoneous
 
Allpair shortest path problem
Allpair shortest path problemAllpair shortest path problem
Allpair shortest path problemArunaP47
 

Ähnlich wie CPSC125 ch6 sec3 (20)

Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
04 greedyalgorithmsii
04 greedyalgorithmsii04 greedyalgorithmsii
04 greedyalgorithmsii
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
 
Shortest Path Problem
Shortest Path ProblemShortest Path Problem
Shortest Path Problem
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
Shortest path
Shortest pathShortest path
Shortest path
 
DAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptxDAA_Presentation - Copy.pptx
DAA_Presentation - Copy.pptx
 
Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Graph 3
Graph 3Graph 3
Graph 3
 
Allpair shortest path problem
Allpair shortest path problemAllpair shortest path problem
Allpair shortest path problem
 

Mehr von David Wood

Internet of Things (IoT) two-factor authentication using blockchain
Internet of Things (IoT) two-factor authentication using blockchainInternet of Things (IoT) two-factor authentication using blockchain
Internet of Things (IoT) two-factor authentication using blockchainDavid Wood
 
Returning to Online Privacy?
Returning to Online Privacy?Returning to Online Privacy?
Returning to Online Privacy?David Wood
 
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...David Wood
 
BlockSW 2019 Keynote
BlockSW 2019 KeynoteBlockSW 2019 Keynote
BlockSW 2019 KeynoteDavid Wood
 
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221David Wood
 
Privacy in the Smart City
Privacy in the Smart CityPrivacy in the Smart City
Privacy in the Smart CityDavid Wood
 
Controlling Complexities in Software Development
Controlling Complexities in Software DevelopmentControlling Complexities in Software Development
Controlling Complexities in Software DevelopmentDavid Wood
 
Privacy Concerns related to Verifiable Claims
Privacy Concerns related to Verifiable ClaimsPrivacy Concerns related to Verifiable Claims
Privacy Concerns related to Verifiable ClaimsDavid Wood
 
Implementing the Verifiable Claims data model
Implementing the Verifiable Claims data modelImplementing the Verifiable Claims data model
Implementing the Verifiable Claims data modelDavid Wood
 
So You Wanna be a Startup CTO 20170301
So You Wanna be a Startup CTO 20170301So You Wanna be a Startup CTO 20170301
So You Wanna be a Startup CTO 20170301David Wood
 
Functional manipulations of large data graphs 20160601
Functional manipulations of large data graphs 20160601Functional manipulations of large data graphs 20160601
Functional manipulations of large data graphs 20160601David Wood
 
When Metaphors Kill
When Metaphors KillWhen Metaphors Kill
When Metaphors KillDavid Wood
 
Secularism in Australia
Secularism in AustraliaSecularism in Australia
Secularism in AustraliaDavid Wood
 
Meditations on Writing in Paradoxes, Oxymorons, and Pleonasms
Meditations on Writing in Paradoxes, Oxymorons, and PleonasmsMeditations on Writing in Paradoxes, Oxymorons, and Pleonasms
Meditations on Writing in Paradoxes, Oxymorons, and PleonasmsDavid Wood
 
Building a writer's platform with social media
Building a writer's platform with social mediaBuilding a writer's platform with social media
Building a writer's platform with social mediaDavid Wood
 
Summary of the Hero's Journey
Summary of the Hero's JourneySummary of the Hero's Journey
Summary of the Hero's JourneyDavid Wood
 
Open by Default
Open by DefaultOpen by Default
Open by DefaultDavid Wood
 
Lod Then, Now and Next 20110926
Lod Then, Now and Next 20110926Lod Then, Now and Next 20110926
Lod Then, Now and Next 20110926David Wood
 
Linked Data ROI 20110426
Linked Data ROI 20110426Linked Data ROI 20110426
Linked Data ROI 20110426David Wood
 
Introduction to Linked Data: RDF Vocabularies
Introduction to Linked Data: RDF VocabulariesIntroduction to Linked Data: RDF Vocabularies
Introduction to Linked Data: RDF VocabulariesDavid Wood
 

Mehr von David Wood (20)

Internet of Things (IoT) two-factor authentication using blockchain
Internet of Things (IoT) two-factor authentication using blockchainInternet of Things (IoT) two-factor authentication using blockchain
Internet of Things (IoT) two-factor authentication using blockchain
 
Returning to Online Privacy?
Returning to Online Privacy?Returning to Online Privacy?
Returning to Online Privacy?
 
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...
Methods for Securing Spacecraft Tasking and Control via an Enterprise Ethereu...
 
BlockSW 2019 Keynote
BlockSW 2019 KeynoteBlockSW 2019 Keynote
BlockSW 2019 Keynote
 
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221
Returning to Online Privacy - W3C/ANU Future of the Web Roadshow 20190221
 
Privacy in the Smart City
Privacy in the Smart CityPrivacy in the Smart City
Privacy in the Smart City
 
Controlling Complexities in Software Development
Controlling Complexities in Software DevelopmentControlling Complexities in Software Development
Controlling Complexities in Software Development
 
Privacy Concerns related to Verifiable Claims
Privacy Concerns related to Verifiable ClaimsPrivacy Concerns related to Verifiable Claims
Privacy Concerns related to Verifiable Claims
 
Implementing the Verifiable Claims data model
Implementing the Verifiable Claims data modelImplementing the Verifiable Claims data model
Implementing the Verifiable Claims data model
 
So You Wanna be a Startup CTO 20170301
So You Wanna be a Startup CTO 20170301So You Wanna be a Startup CTO 20170301
So You Wanna be a Startup CTO 20170301
 
Functional manipulations of large data graphs 20160601
Functional manipulations of large data graphs 20160601Functional manipulations of large data graphs 20160601
Functional manipulations of large data graphs 20160601
 
When Metaphors Kill
When Metaphors KillWhen Metaphors Kill
When Metaphors Kill
 
Secularism in Australia
Secularism in AustraliaSecularism in Australia
Secularism in Australia
 
Meditations on Writing in Paradoxes, Oxymorons, and Pleonasms
Meditations on Writing in Paradoxes, Oxymorons, and PleonasmsMeditations on Writing in Paradoxes, Oxymorons, and Pleonasms
Meditations on Writing in Paradoxes, Oxymorons, and Pleonasms
 
Building a writer's platform with social media
Building a writer's platform with social mediaBuilding a writer's platform with social media
Building a writer's platform with social media
 
Summary of the Hero's Journey
Summary of the Hero's JourneySummary of the Hero's Journey
Summary of the Hero's Journey
 
Open by Default
Open by DefaultOpen by Default
Open by Default
 
Lod Then, Now and Next 20110926
Lod Then, Now and Next 20110926Lod Then, Now and Next 20110926
Lod Then, Now and Next 20110926
 
Linked Data ROI 20110426
Linked Data ROI 20110426Linked Data ROI 20110426
Linked Data ROI 20110426
 
Introduction to Linked Data: RDF Vocabularies
Introduction to Linked Data: RDF VocabulariesIntroduction to Linked Data: RDF Vocabularies
Introduction to Linked Data: RDF Vocabularies
 

CPSC125 ch6 sec3

  • 1. Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co. MSCS Slides Graph Algorithms
  • 2. Shortest Path Problem ●  Assume that we have a simple, weighted, connected graph, where the weights are positive. Then a path exists between any two nodes x and y. ●  How do we find a path with minimum weight? ●  For example, cities connected by roads, with the weight being the distance between them. ●  The shortest-path algorithm is known as Dijkstra’s algorithm. Section 6.3 Shortest Path and Minimal Spanning Tree 1
  • 3. Shortest-Path Algorithm ●  To compute the shortest path from x to y using Dijkstra’s algorithm, we build a set (called IN ) that initially contains only x but grows as the algorithm proceeds. ●  IN contains every node whose shortest path from x, using only nodes in IN, has so far been determined. ●  For every node z outside IN, we keep track of the shortest distance d[z] from x to that node, using a path whose only non-IN node is z. ●  We also keep track of the node adjacent to z on this path, s[z]. Section 6.3 Shortest Path and Minimal Spanning Tree 2
  • 4. Shortest-Path Algorithm ●  Pick the non-IN node p with the smallest distance d. ●  Add p to IN, then recompute d for all the remaining non-IN nodes, because there may be a shorter path from x going through p than there was before p belonged to IN. ●  If there is a shorter path, update s[z] so that p is now shown to be the node adjacent to z on the current shortest path. ●  As soon as y is moved into IN, IN stops growing. The current value of d[y] is the distance for the shortest path, and its nodes are found by looking at y, s[y], s[s [y]], and so forth, until x is reached. Section 6.3 Shortest Path and Minimal Spanning Tree 3
  • 5. Shortest-Path Algorithm ●  ALGORITHM ShortestPath ShortestPath (n × n matrix A; nodes x, y) //Dijkstra’s algorithm. A is a modified adjacency matrix for a //simple, connected graph with positive weights; x and y are //nodes in the graph; writes out nodes in the shortest path from x // to y, and the distance for that path Local variables: set of nodes IN //set of nodes whose shortest path from x //is known nodes z, p //temporary nodes array of integers d //for each node, the distance from x using //nodes in IN array of nodes s //for each node, the previous node in the //shortest path integer OldDistance //distance to compare against //initialize set IN and arrays d and s IN = {x} d[x] = 0 Section 6.3 Shortest Path and Minimal Spanning Tree 4
  • 6. Shortest-Path Algorithm for all nodes z not in IN do d[z] = A[x, z] s [z] = x end for//process nodes into IN while y not in IN do //add minimum-distance node not in IN p = node z not in IN with minimum d[z] IN = IN ∪ {p} //recompute d for non-IN nodes, adjust s if necessary for all nodes z not in IN do OldDistance = d[z] d[z] = min(d[z], d[ p] + A[ p, z]) if d[z] != OldDistance then s [z] = p end if end for end while Section 6.3 Shortest Path and Minimal Spanning Tree 5
  • 7. Shortest-Path Algorithm //write out path nodes write(“In reverse order, the path is”) write (y) z = y repeat write (s [z]) z = s [z] until z = x // write out path distance write(“The path distance is,” d[y]) end ShortestPath Section 6.3 Shortest Path and Minimal Spanning Tree 6
  • 8. Shortest-Path Algorithm Example ●  Trace the algorithm using the following graph and adjacency matrix: ●  At the end of the initialization phase: Section 6.3 Shortest Path and Minimal Spanning Tree 7
  • 9. Shortest-Path Algorithm Example ●  The circled nodes are those in set IN. heavy lines show the current shortest paths, and the d-value for each node is written along with the node label. Section 6.3 Shortest Path and Minimal Spanning Tree 8
  • 10. Shortest-Path Algorithm Example ●  We now enter the while loop and search through the d-values for the node of minimum distance that is not in IN. Node 1 is found, with d[1] = 3. ●  Node 1 is added to IN. We recompute all the d-values for the remaining nodes, 2, 3, 4, and y. p = 1 IN = {x,1} d[2] = min(8, 3 + A[1, 2]) = min(8, ∞) = 8 d[3] = min(4, 3 + A[1, 3]) = min(4, 9) = 4 d[4] = min(∞, 3 + A[1, 4]) = min(∞, ∞) = ∞ d[y] = min(10, 3 + A[1, y]) = min(10, ∞) = 10 ●  This process is repeated until y is added to IN. Section 6.3 Shortest Path and Minimal Spanning Tree 9
  • 11. Shortest-Path Algorithm Example ●  The second pass through the while loop: ■  p = 3 (3 has the smallest d-value, namely 4, of 2, 3, 4, or y) ■  IN = {x, 1, 3} ■  d[2] = min(8, 4 + A[3, 2]) = min(8, 4 + ∞) = 8 ■  d[4] = min(∞, 4 + A[3, 4]) = min(∞, 4 + 1) = 5 (a change, so update s[4] to 3) ■  d[y] = min(10, 4 + A[3, y]) = min(10, 4 + 3) = 7 (a change, so update s[y] to 3) Section 6.3 Shortest Path and Minimal Spanning Tree 10
  • 12. Shortest-Path Algorithm Example ●  The third pass through the while loop: ■  p = 4 (d-value 5) ■  IN = {x, 1, 3, 4} ■  d[2] = min(8, 5 + 7) = 8 ■  d[y] = min(7, 5 + 1) = 6 (a change, update s[y]) Section 6.3 Shortest Path and Minimal Spanning Tree 11
  • 13. Shortest-Path Algorithm Example ●  The third pass through the while loop: ■  p = y ■  IN = {x, 1, 3, 4, y} ■  d[2] = min(8, 6 ) = 8 ●  y is now part of IN, so the while loop terminates. The (reversed) path goes through y, s[y]= 4, s[4]= 3, and s [3] = x. Section 6.3 Shortest Path and Minimal Spanning Tree 12
  • 14. Shortest-Path Algorithm Analysis ●  ShortestPath is a greedy algorithm - it does what seems best based on its limited immediate knowledge. ●  The for loop requires Θ(n) operations. ●  The while loop takes Θ(n) operations. ●  In the worst case, y is the last node brought into IN, and the while loop will be executed n - 1 times. ●  The total number of operations involved in the while loop is Θ(n(n - 1)) = Θ(n2 ). ●  Initialization and writing the output together take Θ(n) operations. ●  So the algorithm requires Θ(n + n2) = Θ(n2 ) operations in the worst case. Section 6.3 Shortest Path and Minimal Spanning Tree 13
  • 15. Minimal Spanning Tree Problem ●  DEFINITION: SPANNING TREE A spanning tree for a connected graph is a nonrooted tree whose set of nodes coincides with the set of nodes for the graph and whose arcs are (some of) the arcs of the graph. ●  A spanning tree connects all the nodes of a graph with no excess arcs (no cycles). There are algorithms for constructing a minimal spanning tree, a spanning tree with minimal weight, for a given simple, weighted, connected graph. ●  Prim’s Algorithm proceeds very much like the shortest- path algorithm, resulting in a minimal spanning tree. Section 6.3 Shortest Path and Minimal Spanning Tree 14
  • 16. Prim’s Algorithm ●  There is a set IN, which initially contains one arbitrary node. ●  For every node z not in IN, we keep track of the shortest distance d[z] between z and any node in IN. ●  We successively add nodes to IN, where the next node added is one that is not in IN and whose distance d[z] is minimal. ●  The arc having this minimal distance is then made part of the spanning tree. ●  The minimal spanning tree of a graph may not be unique. ●  The algorithm terminates when all nodes of the graph are in IN. ●  The difference between Prim’s and Dijkstra’s algorithm is how d[z] (new distances) are calculated. Dijkstra’s: d[z] = min(d[z], d[p] + A[ p, z]) Prim’s: d[z] = min(d[z], A[ p, z])) Section 6.3 Shortest Path and Minimal Spanning Tree 15