Data Structures ( 数据结构 ) Chapter 10:Graphs. Vocabulary Graph 图 Vertex 顶点 Edge 边 Arc 弧 Directed Graph 有向图 Undirected Graph 无向图 Adjacent Vertices 邻接点 Path.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
Graphs.
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.
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.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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 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
Chapter 2 Graph Algorithms.
© 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,
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,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
 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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
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.
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.
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. 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.
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.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Chapter 12 Abstract Data Type.
Data Structures Graphs - Terminology
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Data Structures ( 数据结构 ) Chapter 10:Graphs

Vocabulary Graph 图 Vertex 顶点 Edge 边 Arc 弧 Directed Graph 有向图 Undirected Graph 无向图 Adjacent Vertices 邻接点 Path 路径 Cycle 圈 Strongly connected 强连 通 Weekly connected 弱连通 Disjoint 未连通 depth-first traversal 深度 优先遍历 Breadth-first traversal 广 度优先遍历 Adjacency matrix 邻接矩阵 Adjacency list 邻接表 Minimum Spanning tree 最小派生树

Introduction to Graphs  Linear List  Tree  Graph Recall that a list is a collection of components in which 1.each component (except one, the first) has exactly 1 predecessor. 2.each component (except one, the last) has exactly 1 successor.  multiple successors  Unique predecessor Figure 7-1 A tree Each node may have multiple successors as well as multiple predecessors

Definition of graph vertices edges arc  A Graph is a collection of nodes, called vertices, and a collection of line segments, called edges (or arc), that connecting pairs of vertices. verticeslines  In other words, a graph consists of two sets, a set of vertices and a set of lines.

Terminology directedundirected Graph may be either directed or undirected:  Directed graph(Digraph) direction Each line has a direction (arrow head)to its successor. arcs The lines in a directed graph are known as arcs G = (V, E),V:aggregate of Vertices, E:aggregate of edges 。 ≠ 。 5

Terminology directedundirected Graph may be either directed or undirected:  Undirected graph no direction Each line has no direction. edges The lines in a undirected graph are known as edges G = (V,E), V:aggreaget of Vertices, E:aggreage of edges. = 。

Terminology Terminology Path Path: a sequence of vertices  A path is a sequence of vertices in which each vertex is adjacent to the next one. In the following figure, {A, B, C, E} is one path and {A, B, E, F} is another.  Both directed and undirected graphs have paths.

Terminology Terminology Cycle Cycle path starts and ends with the same vertex  A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. In the following subfigure (b), B, C, D, E, B is a cycle. follow the direction In a digraph, a path can only follow the direction of the arcs move in either direction In an undirected graph, a path can move in either direction along the edge Cycle?

Terminology Loop Loop a single arc beings and ends with the same vertex  A loop is a special case of a cycle in which a single arc beings and ends with the same vertex. Connected Connected  Two vertices are said to be connected if there is a path between them.  A graph is said to be connected if there is a path from any vertex to any other vertex. ConnectedUnconnected

Terminology Connected Connected (directed graph)  Strongly connected there is a path from each vertex to every other vertex A directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph.  Weakly connected at least two vertices are not connected A directed graph is weakly connected if at least two vertices are not connected.  Disjoint A graph is disjoint if it is not connected.

Terminology Degree Degree degree  The degree of a vertex is the number of lines incident to it. The degrees of the nodes A, C, D, F = 1 The degrees of the nodes B, E = 3

Terminology degree of the indegree and outdegree  The degree of a vertex is the sum of the indegree and outdegree of lines incident to it. outdegreethe number of arcs leaving the vertex The outdegree of a vertex in a digraph is the number of arcs leaving the vertex. indegreethe number of arcs entering the vertex The indegree is the number of arcs entering the vertex. 12

Operations Add Vertex Add Vertex

Operations Delete Vertex Delete Vertex Add edge Add edge

Operations Delete edge Delete edge Find vertex Find vertex

Traverse Graph Traverse graph Traverse graph each vertex of the graphs be processed once and only once we must ensure that we process the data in each vertex only once. There are multiple paths to a vertex, we use a visited flag at each vertex to solve this problem.  Depth-first Traversal  Breadth-first Traversal

Depth-first Traversal We process all of a vertex’s descendents before we move to an adjacent vertex. Depth-first traversal of a tree

Depth-first Traversal depth-first traversal of a graph  processing the first vertex  Select any vertex adjacent to the first vertex and process it  Select an adjacent vertex until we reach a vertex with no adjacent entries, back out of the structure.(stack) The order in which the adjacent vertices are processed depends on how the graph is physically stored. In the depth –first traversal all of a node’s descendents are processed before moving to an adjacent

Depth-first Traversal

We Begin by pushing the first vertex A into the stack We then loop, pop the stack, and, after processing the vertex. Push all of the adjacent vertices into the stack. Such as process Vertex X at step 2, we pop x from the stack process it, and then push the adjacent vertices G and H into the stack. When the stack is empty, the traversal is completes.

Breadth-first Traversal We processing all adjacent vertices of a vertex before going to the next level Breadth-first traversal of a tree

Breadth-first Traversal Breadth-first traversal of a graph  processing the first vertex  Processing all of the first adjacent vertices  Pick the first adjacent vertex and processing all of its adjacent vertices, then the second adjacent vertex and so forth until we finished.(Queue) In the Breadth –first traversal all adjacent vertices are processed before processing the descendents of a vertex.

Breadth-first Traversal

We begin by enqueuing vertex A in the queue We the loop, dequeuing the queue and processing the vertex from the front of the queue. After processing the vertex, we place all of its adjacent vertices into the queue. When the queue is empty, the traversal is complete

Graph Storage Structure Represent a graph we need to store two sets  The vertices of the graph  The edges or arcs of the graph Two most common structures  Arrays  Linked list

Adjacency Matrix One-dimensional array to store the vertices Two-dimensional array to store the edges or arcs

Adjacency Matrix

Adjacency List Two-dimensional linked list to store the edges or arcs

Graph Algorithms Graph data Structure

Graph Algorithms Create Graph Insert Vertex Delete Vertex Insert Arc Delete Arc Retrieve Vertex First Arc Traverse

Depth-first Traversal Algorithm Algorithm depthfirst (val graph Processing the keys of the graph is depth-first order. Pre graph is a pointer to a graph head structure Post vertices “processed” 1 If (empty graph) 1Return Set processed flags to not processed 2 walkPtr=graph.first 3Loop (walkPtr) 1 walkPtr->processed = 0 2 walkPtr =walkPtr- >nextVertex 4End loop Process each vertex in list 5createStack(stack) 6 walkPtr=graph.first 7 loop(walkPtr not null) 1 if (walkPtr->Processed <2) 1 if (walkPtr->processed <1) Push and set flag to stack 1 puchStack(stack,walker) 2 walkPtr->processed =1 2 end if Process vertex at stack top 3 loop (not emptyStack(stack)) 1 popStack(stack,vertexPtr) 2 process(vertex->dataPtr) 3 vertexPtr->processed =2 Push all Vertices from adjacency list 4 arcwalkPtr=vertexPr->arc 5 loop( arcwalkPtr not null) 1 vertToPtr=arcwalkPtr->destination 2 if (vertToPtr->processed is 0) 1 puchStack(sack,VertToPtr) 2 vertToPtr->Processed =1 3 end if 4 arcwalkPtr=arcwalkPtr->nextArc 6 end loop 4 end loop 2 end if 3 walkPtr-walkPtr->nextVertex 8 end loop 9 destroyStack(stack) 10Return End depthfirst

Breadth-first Traversal Algorithm Algorithm Breadthfirst (val graph Processing the keys of the graph is Breadth-first order. Pre graph is a pointer to a graph head structure Post vertices “processed 1 If (empty graph) 1 return 2 End if Fist se all processed flags to not processed Falg:0– not processed, 1– enqueued, 2– processed 3 createqueue(queue) 4 walkPtr=graph.first 5 Loop (walkPtr not null) 1 walkPtr->processed =0 2 walkPtr=walkPtr->nextVertex 6 End loop Process each vertex in vertex list 7 walkPtr =graph.first 8 Loop (walkPtr not null) 1 if (walkPtr->Processed <2) 1 if (walkPtr->Processed <1) Enqueue and set processed flag to 1 1 enqueue(queue,walkPtr) 2 walkPtr->Processed =1 2 end if How process descendents of vertex at queue first 3 loop (not emptyQueue(queue)) 1 dequeue(queue,vertexPtr) Process Vertex and flag as processed 2 process(vertexPtr) 3 vertxPtr->processed =2 Enqueue all vertices from adjacency list 4 arcPtr=vertexPtr->arc 5 loop (arcPtr not null) 1 toPtr =arcPtr->destination 2 if (toPtr -> processed =1) 1 enqueue(queue,toPtr) 2 toPtr->processed =1 3 end if 4 arcPtr=arcPtr->nextArc 6 end loop 4 end loop 2 end if 3 walkPtr=walPtr->nextVertex 9 end loop 10 destroyQueue(queue) 11 return End breadthfirst

Networks A network is a graph whose lines are weighted. It is also known as a weighted graph. City airline Network

Minimum Spanning Tree A spanning tree is a tree that contains all of the vertices in the graph A minimum spanning tree of a network such that the sum of its weights are guaranteed to be minimal. if there are duplicate weights, then these may be one or more minimum spanning tree.

Minimum Spanning Tree City airline Network

Minimum Spanning Tree

From all the vertices in the tree. Select the edge with minimal value to a vertex not currently in the tree and insert it into the tree.

Shortest path We find the shortest path between to vertices in network The Dijkstra algorithm is used to find the shortest path between any two nodes in a graph Example : we need to find the shortest path from vertex A to any other vertex in the graph.

Shortest path

Insert the first vertex into the tree From every vertex already in the tree, examine the total path length to all adjacent vertices not in the tree. Select the edge with the minimum total path weight and insert it into the tree Repeat step 2 until all vertices are in the tree

Summary A graph is a collection of nodes, called vertices, and a collection of line segments connection pairs of nodes, called edges or arcs. Graphs may be directed or undirected. A directed graph, or digraph is a graph is which each line has s direction. An undirected graph is a graph in which there is no direction on the lines. A line in a directed graph is called an arc. In a graph, two vertices are said to be adjacent if an edge directly connects them A path is a sequence of vertices in which each vertex is adjacent to the next one A cycle is a path of at least three vertices that starts and ends with the same vertex A loop is a special case of a cycle is which a single arc begins with the same vertex

Summary A graph is said to be connected if,for any two vertices, there is a path from one to the other. A graph is disjointed if it is not connected. The degree of a vertex is the number of the vertices adjacent to it. The outdegree of a vertex is the number of arcs leaving the node; the indegree of a vertex is the number of arcs entering the node. Six operations have been defined for a graph:add a vertex, delete a vertex, add an edge, delete an edge, find a node, and traverse the graph. There are two standard graph traversals: depth- first and breadth first.  In the depth-first traversal, all of the node’s descendents are processed before moving to an adjacent node  In the breadth-first traversal, all of the adjacent vertices are processed before processing the descendents of a vertex

Summary To represent a graph in a computer, we need to store two sets of information: the first sets represents the vertices and the second sets represents the edges. The most common methods used to store a graph are the adjacency matrix method and the adjacency list methods A network is a graph whose lines are weighted. A spanning tree is a graph whose lines are weighted A minimum spanning tree is a spanning tree in which the total weight of the edges is the minimum. Another common algorithm in a graph is to find the shotest pathe between two vertices.