1 directed graphs chapter 8. 2 objectives you will be able to: say what a directed graph is....
DESCRIPTION
3 Directed Graphs A directed graph A finite set of elements Called vertices or nodes Can hold values A finite set of directed Arcs or edges Connect pairs of vertices Often called a digraph.TRANSCRIPT
![Page 1: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/1.jpg)
1
Directed Graphs
Chapter 8
![Page 2: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/2.jpg)
2
Objectives
You will be able to: Say what a directed graph is. Describe two ways to represent a directed
graph: Adjacency matrix. Adjacency lists.
Describe two ways to traverse a directed graph: Depth first search Breadth first search
Manually perform each kind of traversal on a directed graph on paper.
![Page 3: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/3.jpg)
3
Directed Graphs
A directed graph A finite set of elements
Called vertices or nodes Can hold values
A finite set of directed Arcs or edges Connect pairs of vertices
Often called a digraph.
![Page 4: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/4.jpg)
4
Directed Graphs
Applications of directed graphs Analyze electrical circuits Find shortest routes Develop project schedules State diagrams
![Page 5: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/5.jpg)
5
Graph Terminology
Multigraph A digraph in which there can be more than
one arc between a given pair of nodes.
Pseudograph A multigraph in which there can be an arc
from a node back to itself.
Graph Arcs are bidirectional
![Page 6: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/6.jpg)
6
Directed Graphs Trees are a special kind of directed
graph. One node (the root) has no incoming edge. Every other node can be reached from the
root by a unique path.
Graphs differ from trees as ADTs Insertion of a node does not require an
incoming edge … or may have multiple edges.
![Page 7: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/7.jpg)
7
Directed Graph as an ADT
A directed graph is defined as a collection of data elements: Called nodes or vertices And a finite set of direct arcs or edges
Ordered pairs of nodes.
Operations include Constructors Insert node, edge Delete node, edge Search for a value in a node, starting from a
given node
![Page 8: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/8.jpg)
8
Directed Graph Terminology
Weighted digraph Each arc has a "cost" or "weight" Example: Distance between cities
connected by highways.
Classic problem: Find the shortest route from one city to
another.
![Page 9: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/9.jpg)
9
Directed Graph Terminology
A complete digraph Has an edge between each pair of
vertices. (Each direction)
N nodes will have N * (N – 1) edges
![Page 10: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/10.jpg)
10
Directed Graph Representation
Adjacency matrix representation Identify nodes with consecutive integers
1, 2, … n The adjacency matrix is an n by n
matrix. Call it adj
adj[i,j] is• 1 (true) if vertex j is adjacent to vertex i
• There is a directed arc from i to j• 0 (false) otherwise
Direction matters!
![Page 11: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/11.jpg)
11
Graph Representation
1 2 3 4 51 0 1 1 0 12 0 0 1 0 03 0 0 0 1 04 0 0 1 0 05 0 0 0 0 0
rows i
“From” Vertex
columns j
“To” Vertex
• Entry [ 1, 5 ] set to true
• Edge from vertex 1 to vertex 51
10
![Page 12: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/12.jpg)
12
Adjacency Matrix Terminology
Out-degree of ith vertex (node) Number of arc emanating from that
node Sum of 1's in row i
In-degree of jth vertex (node) Number of arcs coming into that node Sum of the 1's in column j
![Page 13: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/13.jpg)
13
Adjacency Matrix
Consider the sum of the products of the pairs of elements from row i and column j
1 2 3 4 51 0 1 1 0 12 0 0 1 0 03 0 0 0 1 04 0 0 1 0 05 0 0 0 0 0
1 2 3 4 51 12345
adj adj 2
This is the number of paths of length 2 from
node 1 to node 3
01
1
![Page 14: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/14.jpg)
14
Adjacency Matrix
This is matrix multiplication What is adj 3?
The value in each entry would represent The number of paths of length 3 From node i to node j
Consider the meaning of the generalization of adj n
![Page 15: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/15.jpg)
15
Adjacency Matrix
Deficiencies in adjacency matrix representation
Data must be stored in separate matrix
When there are few edges the matrix is sparse.
Wasted space
data =
![Page 16: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/16.jpg)
16
Adjacency List Representation
Solving problem of wasted space Better to use an array of pointers to linked row-lists.
This is called an Adjacency List representation.
![Page 17: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/17.jpg)
17
Searching a Graph
Recall that with a tree we search from the root.
But with a digraph … There is no distinguished vertex. There may not be a vertex from which every
other vertex can be reached. May not be possible to traverse entire digraph
(regardless of starting vertex.)
![Page 18: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/18.jpg)
18
Searching a Graph
We must determine which nodes are reachable from a given node
Two standard methods of searching: Depth first search Breadth first search
![Page 19: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/19.jpg)
19
Depth-First Search
Start from a given vertex v Visit first neighbor w, of v Then visit first neighbor of w which has
not already been visited. Continue descent until we reach a node with no
unvisited neighbors.
When no unvisited neighbors Back up to last visited node Visit next unvisited neighbor of that node
![Page 20: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/20.jpg)
20
Depth-First Search
Same as pre-order traversal of a tree except we have to keep track of nodes visited and avoid going back to them.
![Page 21: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/21.jpg)
21
Depth-First Search
Start from node A. What is the sequence of nodes that
would be visited in depth first search?
Click for answer
A, B, E, F, H, C, D, G
Same as pre-order traversal of the tree.
![Page 22: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/22.jpg)
22
A, B, F, G, C, D, H, I, E
Depth-First Search
Start from node A. What is the sequence of nodes that
would be visited in DFS?
Click for answer
![Page 23: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/23.jpg)
23
Depth-First Search
DFS uses backtracking to return to vertices that were seen earlier and already processed or skipped over on an earlier pass
Recursion is a natural technique for this task.
![Page 24: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/24.jpg)
24
Depth-First Search
Algorithm to perform DFS search of digraph from a specified starting vertex
1. Visit the start vertex, v
2. For each vertex w adjacent to v do:If w has not been visited,
apply the depth-first search algorithmwith w as the start vertex.
Note the recursion
![Page 25: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/25.jpg)
25
Breadth-First Search
A different search technique
At each point in the search, visit all previously unvisited neighbors of current node before advancing to their neighbors.
![Page 26: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/26.jpg)
26
Breadth-First Search
Start from a given vertex v Visit all neighbors of v Then visit all previously unvisited neighbors
of first neighbor w of v. Then visit all previously unvisited neighbors
of second neighbor x of v … etc.
Continue, visiting all vertices at distance N from starting vertex before moving on to vertices at distance N+1.
![Page 27: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/27.jpg)
27
Breadth-First Search
Start from node containing A What is a sequence of nodes which
would be visited in BFS?
Click for answer
A, B, D, E, F, C, H, G, I
![Page 28: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/28.jpg)
28
Breadth-First Search
Notice distances from starting node.
A B D E F C H G I0 1 2 3
![Page 29: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/29.jpg)
29
Breadth-First Search
Breadth-First Search defines a tree consisting of nodes reachable from the starting node.
A B D E F C H G I0 1 2 3
![Page 30: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/30.jpg)
30
Breadth-First Search Algorithm
While visiting each node on a given level store its ID so that we can return to it after
completing this level. So that nodes adjacent to it can be visited.
First node visited on given level should befirst node to which we return upon completion of that level.
What data structure does this imply?
A queue
![Page 31: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/31.jpg)
31
Breadth-First Search Algorithm
Algorithm for BFS search of a digraph from a given starting vertex:
1. Visit the start vertex.2. Initialize queue to contain only the start
vertex.3. While queue not empty do
a. Remove a vertex v from the queue.b. For all vertices w adjacent to v do:
If w has not been visited then:i. Visit w.ii. Add w to queue.
End whileEnd of section
![Page 32: 1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency](https://reader036.vdocuments.net/reader036/viewer/2022081605/5a4d1ad17f8b9ab059971691/html5/thumbnails/32.jpg)
32
Directed Graph Traversal
Algorithm to traverse digraph must: Visit each vertex exactly once. BFS or DFS forms basis of traversal. Mark vertices when they have been visited.
1. Initialize an array (vector) visited. visited[i] = false for each vertex i
2. While some element of visited is falsea. Select an unvisited vertex v.b. Set visited[v] to true.c. Use BFS or DFS to visit all vertices reachable from
vEnd while