Graphs Chapter 11 Objectives Upon completion you will be able to:

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 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.
Data Structures ( 数据结构 ) Chapter 10:Graphs. Vocabulary Graph 图 Vertex 顶点 Edge 边 Arc 弧 Directed Graph 有向图 Undirected Graph 无向图 Adjacent Vertices 邻接点 Path.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Introduction to Graphs
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 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 Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Computer Science: A Structured Programming Approach Using C Graphs A graph is a collection of nodes, called vertices, and a collection of segments,
COSC 2007 Data Structures II Chapter 14 Graphs I.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
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. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Data Structures( 数据结构 ) Course 12:Graphs. 2 西南财经大学天府学院 Vocabulary Graph 图 Vertex 顶点 Edge 边 Arc 弧 Directed Graph 有向图 Undirected Graph 无向图 Adjacent Vertices.
Chapter 12 Abstract Data Type.
Data Structures Graphs - Terminology
Introduction to Graphs
Introduction to Graphs
C.Eng 213 Data Structures Graphs Fall Section 3.
CS120 Graphs.
Refresh and Get Ready for More
Graphs Chapter 13.
Graphs.
Chapter 11 Graphs.
Graphs.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs.
Graphs.
Graphs.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Graphs.
Heaps Chapter 6 Section 6.9.
Introduction to Graphs
Presentation transcript:

Graphs Chapter 11 Objectives Upon completion you will be able to: Understand and use basic graph terminology and concepts Define and discuss basic graph and network structures Design and implement graph and network applications Design and implement applications using the graph ADT Define and discuss Dijkstra's shortest path algorithm Data Structures: A Pseudocode Approach with C, Second Edition

11-1 Basic Concepts Directed and Undirected Graphs Cycles and Loops A graph is a collection of nodes, called vertices, and a collection of segments, called lines, connecting pairs of vertices. In Basic Concepts we develop the terminology and structure for graphs. In addition to the graph definitions, discussion points include: Directed and Undirected Graphs Cycles and Loops Connected and Disjoint Graphs Data Structures: A Pseudocode Approach with C, Second Edition

Introduction in the last chapter, we turn our attention to a data structure, graphs, that differs from all of the others in one major concept: each node may have multiple predecessors as well as multiple successors graphs are very useful structures they can used to solve complex routing problems such as designing and routine airlines among the airports they serve they can be used to route messages over a computer network from one node to another Data Structures: A Pseudocode Approach with C, Second Edition

Terminology a graph consists of 2 sets a collection of nodes, called vertices and a collection of line segment connecting pairs of vertices, called lines a directed graph, or digraph for short, is a graph in which each line has a direction (arrow head) to its successor the lines in a directed graph are know as arcs Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (2) in a directed graph, the flow along the arcs between two vertices can follow only the indicated direction an undirected graph is a graph in which there is no direction on the lines, know as edges in an undirected graph, the flow between two vertices can go in either direction Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (3) two vertices in a graph are said to be adjacent vertices (neighbors) if there exists an edge that directly connects them from figure 11-1, A and B are adjacent D and F are not adjacent a path is a sequence of vertices in which each vertex is adjacent to the next one (A, B, C, E ) is one path (A, B, E, F) is another path Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (4) A cycle is a path consisting of at least 3 vertices that starts and ends with the same vertex figure 11-1 (b) B, C, D, E, B is a cycle (a) does not constitute a cycle a loop is a special case of a cycle in which a single arc begins and ends with the same vertex in a loop, the end points of the edge are the same Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (5) two vertices are said to be connected if there is a path between them a graph is said to be connected if, suppressing direction, there is a path from any vertex any other vertex furthermore, a directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (6) a directed graph is weakly connected, when there are at least two vertices that are not connected a graph is disjoint if it is not connected figure 11-2 contains a weakly connected graph (a) a strong connected graph (b) and disjoint graph (c) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Terminology (7) the degree of a vertex is the number of lines incident to it figure 11-2(a), the degree of vertex B is 3 and the degree of vertex E is 4 the outdegree of a vertex in a digraph is the number of arcs leaving the vertex the indegree is the number of arcs entering the vertex Data Structures: A Pseudocode Approach with C, Second Edition

11-2 Operations Insert Vertex Delete Vertex Add Edge Delete Edge We define and discuss the six primitive graph operations required to maintain a graph. Insert Vertex Delete Vertex Add Edge Delete Edge Find Vertex Traverse Graph Data Structures: A Pseudocode Approach with C, Second Edition

Add vertex given a graph, add vertex inserts a new vertex into the graph when a vertex is added, it is disjoint (it is not connected to any other vertices in the list) after a vertex is added, it must be connected Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Delete vertex delete vertex removes a vertex from the graph when a vertex is deleted all connecting edges are also removed Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Add edge add an edge connects a vertex to a destination vertex if a vertex requires multiple edges, then add an edge must be called once for each adjacent vertex to ad an edge, two vertices must be specified the graph is a digraph, then one of the vertices must be specified as the source and one as the destination Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Delete edge delete an edge removes one edge from a graph figure 11-6 contains an example, deleting the edge {A, E} from the graph Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Find Vertex find vertex traverses a graph looking for a specified vertex if the vertex is found, its address is returned if it is not found, an error is returned Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Traverse Graph given a graph, there is always at least one application that requires that all vertices in the graph be visited that is there is at least one application that requires that the graph be traversed because a vertex in a graph can have multiple parents, the traversal of a graph presents some problems not found in the traversal of linear lists and trees Data Structures: A Pseudocode Approach with C, Second Edition

Traverse Graph (2) specifically, we must somehow assure that we process the data in each vertex only once because there are multiple paths to a vertex, it is possible that we can arrive at it from more than one direction as we traverse the graph the traditional solution to this problem is to include a visited flag at each vertex before the traversal, the graph is scanned and the visited flag is set off Data Structures: A Pseudocode Approach with C, Second Edition

Traverse Graph (3) then, as we traverse the graph, we set the visited flag on to indicate the at the data have already been processed there are 2 standard graph traversal depth–first traversal breadth-first traversal Data Structures: A Pseudocode Approach with C, Second Edition

Depth-first traversal in the depth-first traversal, all of a vertex’s descendents are processed before we move to an adjacent vertex the depth-first traversal of a graph starts by processing the first vertex of the graph after processing the first vertex, we select any vertex adjacent to the first vertex and process it as each vertex is processed, we select an adjacent vertex until we reach a vertex with no adjacent entries Data Structures: A Pseudocode Approach with C, Second Edition

Depth-first traversal (2) this is similar to reaching a leaf in a tree we then back out of the structure, processing adjacent vertices as we go it should be obvious that this logic requires a stack (or recursion) to complete the traversal the order in which the adjacent vertices are processed depends on how the graph is physically stored Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Breadth-first in the breadth-first traversal of a graph, all adjacent vertices of a vertex are processed before going to the next level the breadth-first traversal of a graph begins by picking a starting vertex and after processing it, we process all of its adjacent vertices when all of the adjacent vertices have been processed, we pick the first adjacent vertex and process all of its vertices, then the second adjacent vertex and process all of its vertices, and so forth until we done Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

11-3 Graph Storage Structures To represent a graph, we need to store two sets. The first set represents the vertices of the graph, and the second set represents the edges or arcs. The two most common structures used to store these sets are arrays and linked lists. Adjacency Matrix Adjacency List Data Structures: A Pseudocode Approach with C, Second Edition

Adjacency matrix the adjacency matrix uses a vector (one-dimensional array) for the vertices and a matrix (2-dimensional array) to store the edges if 2 vertices are adjacent, that is if there is an edge between them, the matrix intersect has a value of 1 if there is no edge between them, the intersect is set to 0 Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Adjacency matrix (2) in addition to the limitation that the size of the graph must be know before the program starts, there is another serious limitation in the adjacency matrix only one edge can be stored between any 2 vertices while this does not prevent many graphs from using the matrix format, some network structures do require multiple edges between vertices Data Structures: A Pseudocode Approach with C, Second Edition

Adjacency list the adjacency list uses a 2-dimensional ragged array to store the edges the vertex list is a singly-linked list of the vertices in the list depending on the application, it could also be implemented using doubly-linked lists or circularly-linked list the pointer at the left of the list links the vertex entries together Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

11-4 Graph Algorithms Create Graph Insert Vertex Delete Vertex In this section we develop a minimum set of algorithms that are needed to create and maintain a directed graph. Create Graph Insert Vertex Delete Vertex Insert Arc Delete Arc Retrieve Vertex Depth-first Traversal Breadth-first Traversal Destroy Graph Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

11-5 Graph ADT Data Structure Functions We begin by developing the code for the three graph structures. We then develop the code for the ADT functions. Data Structure Functions Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

11-6 Networks Minimum Spanning Tree Shortest Path Algorithm A network is a graph whose lines are weighted. It is also known as a weighted graph. Included in this section are two graph applications that process networks. Minimum Spanning Tree Shortest Path Algorithm Data Structures: A Pseudocode Approach with C, Second Edition

Networks (2) example: an airline might use a graph to represent the routes between cities that it serves the cities would be represented by the vertices and the edge a route between two cities the edge’s weight could represent the flight miles between the two cities or it cold represent the price of the flight Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Networks (3) because the weight is an attribute of an edge, it is stored in the structure that contains the edge in an adjacency matrix, the weight would be stored as the intersection value in an adjacency list, it would be stored as the value in the adjacency linked list Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Networks (4) there are 2 application of networks the minimum spanning tree and the shortest path through a network Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Minimum spanning tree given a connected network, we derived one or more spanning trees from the network a spanning tree is a tree that contains all of the vertices in the graph one of the more interesting algs derives the minimum spanning tree of a network such that the sum of its weights are guaranteed to be minimal Data Structures: A Pseudocode Approach with C, Second Edition

Minimum spanning tree (2) if the weights in the network are unique then there will be only one minimum spanning tree if there are duplicate weights then there may be one or more minimum spanning trees there are many applications for minimum spanning tree, all with the requirement to minimize some aspect of the graph such as the distance among all of the vertices in the graph Data Structures: A Pseudocode Approach with C, Second Edition

Minimum spanning tree (3) example: given a network of computers, we could create a tree that connects all of the computers the minimum spanning tree gives us the shortest length of cable that can be used to connect all the computers together while ensuring that there is a path between any 2 computers Data Structures: A Pseudocode Approach with C, Second Edition

Minimum spanning tree (3) to create a minimum spanning tree in a strongly connected network, that is in a network in which there is a path between any 2 vertices, the edges for the minimum spanning tree are chosen so that the following properties exist: every vertex is included the total edge weight of the spanning tree is the minimum possible that includes a path between any 2 vertices Data Structures: A Pseudocode Approach with C, Second Edition

Example from figure 11-20, we can start with any vertex let’s start with A Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Shortest path algorithm an application used with graph requires that we find the shortest path between 2 vertices in a network example: if the network represents the routes flown by an airline, when we travel we would like find the least expensive route between home and our destination the dijkstra alg is used to find the shortest path between any 2 nodes in a graph Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued) Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition