chapter 8, part i graph algorithms. 2 chapter outline graph background and terminology data...
TRANSCRIPT
![Page 1: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/1.jpg)
Chapter 8, Part I
Graph Algorithms
![Page 2: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/2.jpg)
2
Chapter Outline
• Graph background and terminology
• Data structures for graphs
• Graph traversal algorithms
• Minimum spanning tree algorithms
• Shortest-path algorithm
• Biconnected components
• Partitioning sets
![Page 3: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/3.jpg)
3
Prerequisites
• Before beginning this chapter, you should be able to:– Describe a set and set membership– Use two-dimensional arrays– Use stack and queue data structures– Use linked lists– Describe growth rates and order
![Page 4: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/4.jpg)
4
Goals
• At the end of this chapter you should be able to:– Describe and define graph terms and concepts– Create data structures for graphs– Do breadth-first and depth-first traversals and
searches– Find the minimum spanning tree for a connected
graph– Find the shortest path between two nodes of a
connected graph– Find the biconnected components of a connected
graph
![Page 5: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/5.jpg)
5
Graph Types
• A directed graph edges’ allow travel in one direction
• An undirected graph edges’ allow travel in either direction
![Page 6: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/6.jpg)
6
Graph Terminology
• A graph is an ordered pair G=(V,E) with a set of vertices or nodes and the edges that connect them
• A subgraph of a graph has a subset of the vertices and edges
• The edges indicate how we can move through the graph
![Page 7: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/7.jpg)
7
Graph Terminology
• A path is a subset of E that is a series of edges between two nodes
• A graph is connected if there is at least one path between every pair of nodes
• The length of a path in a graph is the number of edges in the path
![Page 8: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/8.jpg)
8
Graph Terminology
• A complete graph is one that has an edge between every pair of nodes
• A weighted graph is one where each edge has a cost for traveling between the nodes
![Page 9: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/9.jpg)
9
Graph Terminology
• A cycle is a path that begins and ends at the same node
• An acyclic graph is one that has no cycles
• An acyclic, connected graph is also called an unrooted tree
![Page 10: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/10.jpg)
10
Data Structures for Graphsan Adjacency Matrix
• A two-dimensional matrix or array that has one row and one column for each node in the graph
• For each edge of the graph (Vi, Vj), the location of the matrix at row i and column j is 1
• All other locations are 0
![Page 11: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/11.jpg)
11
Data Structures for GraphsAn Adjacency Matrix
• For an undirected graph, the matrix will be symmetric along the diagonal
• For a weighted graph, the adjacency matrix would have the weight for edges in the graph, zeros along the diagonal, and infinity (∞) every place else
![Page 12: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/12.jpg)
12
Adjacency Matrix Example 1
![Page 13: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/13.jpg)
13
Adjacency Matrix Example 2
![Page 14: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/14.jpg)
14
Data Structures for GraphsAn Adjacency List
• A list of pointers, one for each node of the graph
• These pointers are the start of a linked list of nodes that can be reached by one edge of the graph
• For a weighted graph, this list would also include the weight for each edge
![Page 15: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/15.jpg)
15
Adjacency List Example 1
![Page 16: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/16.jpg)
16
Adjacency List Example 2
![Page 17: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/17.jpg)
17
Graph Traversals
• We want to travel to every node in the graph
• Traversals guarantee that we will get to each node exactly once
• This can be used if we want to search for information held in the nodes or if we want to distribute information to each node
![Page 18: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/18.jpg)
18
Depth-First Traversal
• We follow a path through the graph until we reach a dead end
• We then back up until we reach a node with an edge to an unvisited node
• We take this edge and again follow it until we reach a dead end
• This process continues until we back up to the starting node and it has no edges to unvisited nodes
![Page 19: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/19.jpg)
19
Depth-First Traversal Example
• Consider the following graph:
• The order of the depth-first traversal of this graph starting at node 1 would be:1, 2, 3, 4, 7, 5, 6, 8, 9
![Page 20: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/20.jpg)
20
Depth-First Traversal Algorithm
Visit( v )
Mark( v )
for every edge vw in G do
if w is not marked then
DepthFirstTraversal(G, w)
end if
end for
![Page 21: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/21.jpg)
21
Breadth-First Traversal
• From the starting node, we follow all paths of length one
• Then we follow paths of length two that go to unvisited nodes
• We continue increasing the length of the paths until there are no unvisited nodes along any of the paths
![Page 22: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/22.jpg)
22
Breadth-First Traversal Example
• Consider the following graph:
• The order of the breadth-first traversal of this graph starting at node 1 would be: 1, 2, 8, 3, 7, 4, 5, 9, 6
![Page 23: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/23.jpg)
23
Breadth-First Traversal Algorithm
Visit( v )Mark( v )Enqueue( v )while the queue is not empty do
Dequeue( x )for every edge xw in G do
if w is not marked thenVisit( w )Mark( w )Enqueue( w )
end ifend for
end while
![Page 24: Chapter 8, Part I Graph Algorithms. 2 Chapter Outline Graph background and terminology Data structures for graphs Graph traversal algorithms Minimum spanning](https://reader035.vdocuments.site/reader035/viewer/2022062304/56649ccf5503460f9499b590/html5/thumbnails/24.jpg)
24
Traversal Analysis
• If the graph is connected, these methods will visit each node exactly once
• Because the most complex work is done when the node is visited, we can consider these to be O(N)
• If we count the number of edges considered, we will find that is also linear with respect to the number of graph edges