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.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Graphs and Digraphs Chapter 14.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Csc 2720 Instructor: Zhuojun Duan
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graph Implementation.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 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 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 Directed Graphs Applications of directed graphs Analyze electrical circuits Find shortest routes Develop project schedules State diagrams

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 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 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 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 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 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 Graph Representation 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

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 Adjacency Matrix Consider the sum of the products of the pairs of elements from row i and column j adj adj 2 This is the number of paths of length 2 from node 1 to node

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 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 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 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 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 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 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 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 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 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 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 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 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 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 Breadth-First Search Notice distances from starting node. A B D E F C H G I

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

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