Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Directed Graphs Chapter 8

2 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.

3 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.

4 4 Directed Graphs Applications of directed graphs Analyze electrical circuits Find shortest routes Develop project schedules State diagrams

5 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

6 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.

7 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

8 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.

9 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

10 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!

11 11 Graph Representation 12345 101101 200100 300010 400100 500000 rows i “From” Vertex columns j “To” Vertex Entry [ 1, 5 ] set to true Edge from vertex 1 to vertex 5 Entry [ 1, 5 ] set to true Edge from vertex 1 to vertex 5 1 1 0

12 12 Adjacency Matrix Terminology Out-degree of i th vertex (node) Number of arc emanating from that node Sum of 1's in row i In-degree of j th vertex (node) Number of arcs coming into that node Sum of the 1's in column j

13 13 Adjacency Matrix Consider the sum of the products of the pairs of elements from row i and column j 12345 101101 200100 300010 400100 500000 12345 11 2 3 4 5 adj adj 2 This is the number of paths of length 2 from node 1 to node 3 0 1 1

14 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

15 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 =

16 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.

17 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.)

18 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

19 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

20 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.

21 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.

22 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

23 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.

24 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: Ifwhas not been visited, apply the depth-first search algorithm with w as the start vertex. Note the recursion

25 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.

26 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.

27 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

28 28 Breadth-First Search Notice distances from starting node. A B D E F C H G I 0 1 2 3

29 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 I 0 1 2 3

30 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 be first node to which we return upon completion of that level. What data structure does this imply? A queue

31 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 while End of section

32 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 false a. Select an unvisited vertex v. b. Set visited[v] to true. c. Use BFS or DFS to visit all vertices reachable from v End while


Download ppt "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."

Similar presentations


Ads by Google