Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 21: Graphs.
Advertisements

Lecture 15. Graph Algorithms
Review Binary Search Trees Operations on Binary Search Tree
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Data Structures Using C++
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
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.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Data Structures Using Java1 Chapter 11 Graphs. Data Structures Using Java2 Chapter Objectives Learn about graphs Become familiar with the basic terminology.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
Introduction to Graphs
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 03 / 25 / 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 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
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.
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,
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Week 11 - Monday.  What did we talk about last time?  Binomial theorem and Pascal's triangle  Conditional probability  Bayes’ theorem.
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.
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.
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,
– Graphs 1 Graph Categories Strong Components Example of Digraph
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Paths and circuits.
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
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 & Algorithm Analysis lec(8):Graph T. Souad alonazi
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures 13th Week
Java Programming: Program Design Including Data Structures
Introduction to Graphs
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPHS.
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:

Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs

2 Objectives Learn about graphs Become familiar with the basic terminology of graph theory Discover how to represent graphs in computer memory Examine and implement various graph traversal algorithms

3 Graph Definitions and Notations Graph G pair –G = (V, E), where V is a finite nonempty set –Called the set of vertices of G, and E   V x V Elements of E –Pairs of elements of V E: set of edges of G G called trivial if it has only one vertex Directed graph (digraph) –Elements in set of edges of graph G: ordered Undirected graph: not ordered

4 FIGURE 12-3 Various undirected graphs FIGURE 12-4 Various directed graphs

5 Graph Definitions and Notations Graph H called subgraph of G –If V(H)  V(G) and E(H)  E(G) –Every vertex of H: vertex of G –Every edge in H: edge in G Graph shown pictorially –Vertices drawn as circles Label inside circle represents vertex Undirected graph: edges drawn using lines Directed graph: edges drawn using arrows

6 Graph Definitions and Notations Let u and v be two vertices in G –u and v adjacent If edge from one to the other exists: (u, v)  E Loop –Edge incident on a single vertex e 1 and e 2 called parallel edges –edges e 1 and e 2 associate with same pair of vertices {u, v} Simple graph –No loops, no parallel edges ABC D ABC D

7 Graph Definitions and Notations Let e = (u, v) be an edge in G –Edge e is incident on the vertices u and v –Degree of u written deg(u) or d(u) Number of edges incident with u Each loop on vertex u –Contributes two to the degree of u u is called an even (odd) degree vertex –If the degree of u is even (odd) ABC D

8 Graph Definitions and Notations Path from u to v –If sequence of vertices u 1, u 2,..., u n exists Such that u = u 1, u n = v and (u i, u i + 1) is an edge for all i =1, 2,..., n – 1 Vertices u and v called connected –If path from u to v exists Simple path –All vertices distinct (except possibly first, last) – A to C / A to B / A to D/ D to C/ D to B/ B to C/ B to D/ C to B / C to D –There is no path to A Cycle in G –Simple path in which first and last vertices are the same –A Cycle such as B C D B ABC D

9 Graph Definitions and Notations G is connected –If path from any vertex to any other vertex exists Let G be a directed graph and let u and v be two vertices in G –If edge from u to v exists: (u, v)  E u is adjacent to v v is adjacent from u »A adjacent to B but B adjacent from A ABC D ABC D

10 Graph Definitions and Notations Definitions of paths and cycles in G –Similar to those for undirected graphs G is strongly connected –If any two vertices in G are connected

11 Graph Representation Graphs represented in computer memory –Two common ways Adjacency matrices Adjacency lists

12 Adjacency Matrices Let G be a graph with n vertices where n > zero Let V(G) = {v 1, v 2,..., v n } –Adjacency matrix

13 Adjacency Lists Given: –Graph G with n vertices, where n > zero –V(G) = {v 1, v 2,..., v n } For each vertex v: linked list exists –Linked list node contains vertex u: (v, u)  E(G) Use array A, of size n, such that A[i] –Reference variable pointing to first linked list node containing vertices to which v i adjacent Each node has two components: vertex, link –Component vertex Contains index of vertex adjacent to vertex i

14 Adjacency Lists (cont’d.) Adjacency list of graph G3 Adjacency list of graph G2 FIGURE 12-4 Various directed graphs

15 Graph Traversals –Similar to traversing a binary tree A bit more complicated Two most common graph traversal algorithms –Depth first traversal –Breadth first traversal

16 Depth First Traversal Similar to binary tree preorder traversal General algorithm A B C DE F G A CD E G F E F B A AC E FGDB H D H H

Data Structures Using C++ 2E17 Depth First Traversal (cont’d.) General algorithm for depth first traversal at a given node v –Recursive algorithm

18 Breadth First Traversal Similar to traversing binary tree level-by-level –Nodes at each level Visited from left to right –All nodes at any level i Visited before visiting nodes at level i + one A B C DE F G A CD E G F E F AC D EGFB B A D H H H

19 Breadth First Traversal in Queue way A B C DE F G AC D EGFB ACDEGFB H H H

20 Breadth First Traversal (cont’d.) General search algorithm –Breadth first search algorithm with a queue

Data Structures Using C++ 2E21

Shortest Path (greedy algorithm) Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph uses the weight to represent the distance between two places Weight of the path P –Given G as a weighted graph with vertices u and v in G and P as a path in G from u to v Sum of the weights of all the edges on the path Shortest path: path with the smallest weight Data Structures Using C++ 2E22

Data Structures Using C++ 2E23 Shortest Path Algorithm (cont’d.) Shortest path algorithm space (greedy algorithm) See code on page 700 –class weightedGraphType Extend definition of class graphType Adds function createWeightedGraph to create graph and weight matrix associated with the graph

Data Structures Using C++ 2E24 Shortest Path General algorithm –Initialize array smallestWeight smallestWeight[u] = weights[vertex, u] –Set smallestWeight[vertex] = zero –Find vertex v closest to vertex where shortest path is not determined –Mark v as the (next) vertex for which the smallest weight is found

Data Structures Using C++ 2E25 Shortest Path (cont’d.) General algorithm (cont’d.) –For each vertex w in G, such that the shortest path from vertex to w has not been determined and an edge (v, w) exists If weight of the path to w via v smaller than its current weight Update weight of w to the weight of v + weight of edge (v, w)

Data Structures Using C++ 2E26 Shortest Path (cont’d.) FIGURE 12-8 Weighted graph G FIGURE 12-9 Graph after Steps 1 and 2 execute

Data Structures Using C++ 2E27 Shortest Path (cont’d.) FIGURE Graph after the first iteration of Steps 3 to 5 FIGURE Graph after the second iteration of Steps 3 to 5

Data Structures Using C++ 2E28 Shortest Path (cont’d.) FIGURE Graph after the fourth iteration of Steps 3 through 5 FIGURE Graph after the third iteration of Steps 3 to 5

Example E 4 A B CD Depth First A B C D E Breadth First A B C E D Shortest Path

30 A B C D E F G Depth First A B D F C G E Breadth First A B C D E G F Shortest Path