Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.

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.
Graph Theory.
Data Structures Using C++
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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++
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
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.
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.
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.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Social Media Mining Graph Essentials.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
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.
Graph Theory Topics to be covered:
© 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,
Graphs Chapter 12.
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,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
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.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Discrete Mathematical Structures: Theory and Applications
Graph Theory and Applications
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 20.
Data Structures 13th Week
Java Programming: Program Design Including Data Structures
CS120 Graphs.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Chapter 11 Graphs.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
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:

Chapter 20: Graphs

Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover how to represent graphs in computer memory – Explore graphs as ADTs – Examine and implement various graph traversal algorithms – Learn how to implement the shortest path algorithm – Examine and implement the minimal spanning tree algorithm 2C++ Programming: Program Design Including Data Structures, Seventh Edition

Introduction Königsberg bridge problem: – The river Pregel flows around the island Kneiphof and then divides into two branches 3C++ Programming: Program Design Including Data Structures, Seventh Edition

Introduction (cont’d.) Starting at one land area, can you cross all bridges exactly once and return to the start? – In 1736, Euler represented the problem as a graph and answered the question: No 4C++ Programming: Program Design Including Data Structures, Seventh Edition

Introduction (cont’d.) Over the past 200 years, graph theory has been applied to a variety of problems, including: Model electrical circuits, chemical compounds, highway maps, etc. Analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science, etc. 5C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations a  X: a is an element of the set X Subset (Y  X): every element of Y is also an element of X Intersection (A  B): contains all the elements in A and B – A  B =  x | x  A and x  B  Union (A  B): set of all the elements that are in A or in B – A  B =  x | x  A or x  B  6C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) A  B: set of all the ordered pairs of elements of A and B – A  B =  (a, b) | a  A, b  B  Graph G: G = (V, E) – V is a finite nonempty set of vertices of G – E  V  V – Elements in E are the pairs of elements of V – E is called set of edges 7C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) Directed graph or digraph: elements of E(G) are ordered pairs Undirected graph: elements not ordered pairs If (u, v) is an edge in a directed graph – Origin: u – Destination: v Subgraph H of G: if V(H)  V(G) and E(H)  E(G) Every vertex and edge of V is in G 8C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) 9C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) 10C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) Adjacent: there is an edge from one vertex to the other; i.e., (u, v)  E(G) Incident: if edge e = (u, v) then e is incident on u and v – Loop: edge incident on a single vertex Parallel edges: associated with the same pair of vertices Simple graph: has no loops or parallel edges 11C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) Path: sequence of vertices u 1, u 2,..., u n such that u = u 1, u n = v, and (u i, u i + 1 ) is an edge for all i = 1, 2,..., n − 1 Connected vertices: there is a path from u to v Simple path: path in which all vertices, except possibly the first and last, are distinct Cycle: simple path in which the first and last vertices are the same 12C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Definitions and Notations (cont’d.) Connected: path exists from any vertex to any other vertex – Component: maximal subset of connected vertices In a connected graph G, if there is an edge from u to v, i.e., (u, v)  E(G), then u is adjacent to v and v is adjacent from u Strongly connected: any two vertices in G are connected 13C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Representation To write programs that process and manipulate graphs – Must store graphs in computer memory A graph can be represented in several ways: – Adjacency matrices – Adjacency lists 14C++ Programming: Program Design Including Data Structures, Seventh Edition

Adjacency Matrix G: graph with n vertices (n  0) – V(G) =  v 1, v 2,..., v n  Adjacency matrix (A G of G): two-dimensional n  n matrix such that: Adjacency matrix of an undirected graph is symmetric 15C++ Programming: Program Design Including Data Structures, Seventh Edition

Adjacency Lists G: graph with n vertices (n  0) – V(G) =  v 1, v 2,..., v n  Linked list corresponding to each vertex, v, – Each node of linked list contains the vertex, u, such that (u,v)  E(G) – Each node has two components, such as vertex and link 16C++ Programming: Program Design Including Data Structures, Seventh Edition

Adjacency Lists (cont’d.) 17C++ Programming: Program Design Including Data Structures, Seventh Edition

Operations on Graphs Operations commonly performed on a graph: – Create the graph – Clear the graph Makes the graph empty – Determine whether the graph is empty – Traverse the graph – Print the graph 18C++ Programming: Program Design Including Data Structures, Seventh Edition

Operations on Graphs (cont’d.) The adjacency list (linked list) representation: – For each vertex, v, vertices adjacent to v are stored in linked list associated with v In a directed graph, vertices adjacent to v are called immediate successors – To manage data in a linked list, use class unorderedLinkedList 19C++ Programming: Program Design Including Data Structures, Seventh Edition

Graphs as ADTs We implement graphs as an abstract data type (ADT), including functions to: – Create/clear the graph – Print the graph – Traverse the graph – Determine the graph’s size 20C++ Programming: Program Design Including Data Structures, Seventh Edition

Graph Traversals Traversing a graph is similar to traversing a binary tree, except that: – A graph might have cycles – Might not be able to traverse the entire graph from a single vertex Most common graph traversal algorithms: – Depth first traversal – Breadth first traversal 21C++ Programming: Program Design Including Data Structures, Seventh Edition

Depth First Traversal Depth first traversal at a given node, v: – Mark node v as visited – Visit the node – for each vertex u adjacent to v if u is not visited start the depth first traversal at u This is a recursive algorithm 22C++ Programming: Program Design Including Data Structures, Seventh Edition

Depth First Traversal (cont’d.) Depth-first ordering of vertices: – 0, 1, 4, 3, 2, 5, 7, 8, 6, 9 Breadth-first ordering of vertices: – 0, 1, 3, 4, 2, 5, 7, 8, 6, 9 23C++ Programming: Program Design Including Data Structures, Seventh Edition

Breadth First Traversal Breadth first traversal of a graph – Similar to traversing a binary tree level by level – Nodes at each level are visited from left to right Starting at the first vertex, the graph is traversed as much as possible – Then go to next vertex not yet visited Use a queue to implement the breadth first search algorithm 24C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm Weight of the edge: nonnegative real number assigned to the edges connecting two vertices Weighted graph: every edge has a nonnegative weight Weight of the path P – Sum of the weights of all edges on the path P – Also called the weight of v from u via P Source: starting vertex in the path 25C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Shortest path: path with the smallest weight Shortest path algorithm – Called the greedy algorithm, developed by Dijkstra – G: graph with n vertices, where n ≥ 0 – V(G) = {v 1, v 2,..., v n } – W: two-dimensional n × n matrix 26C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Shortest path algorithm: 27C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) 28C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) 29C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Graph after first iteration of Steps 3, 4, and 5 30C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Graph after second iteration of Steps 3, 4, and 5 31C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Graph after third iteration of Steps 3, 4, and 5 32C++ Programming: Program Design Including Data Structures, Seventh Edition

Shortest Path Algorithm (cont’d.) Graph after fourth iteration of Steps 3, 4, and 5 33C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree Company needs to shut down a maximum number of connections and still be able to fly from one city to another 34C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree (cont’d.) 35C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree (cont’d.) (Free) tree: simple graph such that if u and v are two vertices in T, then there is a unique path from u to v Rooted tree: tree in which a particular vertex is designated as a root Weighted tree: tree in which a weight is assigned to the edges – Weight of T: sum of the weights of all the edges in T, denoted by W(T) 36C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree (cont’d.) Spanning tree of graph G: if T is a subgraph of G such that V(T) = V(G) – All the vertices of G are in T – Figure shows three spanning trees of the graph shown in Figure Theorem: a graph G has a spanning tree if and only if G is connected Minimal spanning tree: spanning tree in a weighted graph with the minimum weight 37C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree (cont’d.) Two well-known algorithms to find a minimal spanning tree: – Kruskal’s algorithm – Prim’s algorithm Builds the tree iteratively by adding edges until a minimal spanning tree is obtained Start with a designated vertex, called the source vertex At each iteration, a new edge that does not complete a cycle is added to the tree 38C++ Programming: Program Design Including Data Structures, Seventh Edition

Minimal Spanning Tree (cont’d.) 39C++ Programming: Program Design Including Data Structures, Seventh Edition

Prim’s Algorithm to Find a Minimal Spanning Tree 40C++ Programming: Program Design Including Data Structures, Seventh Edition

Prim’s Algorithm to Find a Minimal Spanning Tree (cont’d.) 41C++ Programming: Program Design Including Data Structures, Seventh Edition

Summary A graph G is a pair, G = (V, E) In an undirected graph G = (V, E), the elements of E are unordered pairs In a directed graph G = (V, E), the elements of E are ordered pairs H is a subgraph of G if every vertex of H is a vertex of G and every edge is an edge in G Two vertices in an undirected graph are adjacent if there is an edge between them 42C++ Programming: Program Design Including Data Structures, Seventh Edition

Summary (cont’d.) Loop: an edge incident on a single vertex Simple graph: no loops and no parallel edges Simple path: all the vertices, except possibly the first and last vertices, are distinct Cycle: a simple path in which the first and last vertices are the same An undirected graph is connected if there is a path from any vertex to any other vertex 43C++ Programming: Program Design Including Data Structures, Seventh Edition

Summary (cont’d.) Shortest path algorithm gives the shortest distance for a given node to every other node in the graph In a weighted graph, every edge has a nonnegative weight A tree in which a particular vertex is designated as a root is called a rooted tree A tree T is called a spanning tree of graph G if T is a subgraph of G such that V(T) = V(G) 44C++ Programming: Program Design Including Data Structures, Seventh Edition