Data Structures Using C++

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.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
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.
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.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
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.
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 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) 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.
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
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Graph Theory Topics to be covered:
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,
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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 1 Definition 2 Terminology 3 Properties 4 Internal representation Adjacency list Adjacency matrix 5 Exploration algorithms 6 Other algorithms.
Discrete Mathematical Structures: 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 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 20.
Java Programming: Program Design Including Data Structures
Introduction to Graphs
Graphs: As a mathematics branch
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graph Algorithm.
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Elementary Graph Algorithms
Graphs.
Chapter 11 Graphs.
Chapter 16 1 – Graphs Graph Categories Strong Components
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 Using C++ Chapter 12 Graphs Data Structures Using C++

Data Structures Using C++ Chapter Objectives 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 Data Structures Using C++

Data Structures Using C++ Chapter Objectives Learn how to implement the shortest path algorithm Examine and implement the minimal spanning tree algorithm Explore the topological sort Data Structures Using C++

Königsberg Bridge Problem In 1736, the following problem was posed: River Pregel (Pregolya) flows around the island Kneiphof Divides into two River has four land areas (A, B,C, D) Bridges are labeled a, b, c, d, e, f, g Data Structures Using C++

Data Structures Using C++ Graphs Data Structures Using C++

Königsberg Bridge Problem The Königsberg bridge problem Starting at one land area, is it possible to walk across all the bridges exactly once and return to the starting land area? In 1736, Euler represented Königsberg bridge problem as graph; Answered the question in the negative. This marked (as recorded) the birth of graph theory. Data Structures Using C++

Data Structures Using C++ Graphs Data Structures Using C++

Graph Definitions and Notation A graph is a 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 are pairs of elements of V. E is called the set of edges Data Structures Using C++

Graph Definitions and Notation Let V(G) denote the set of vertices, and E(G) denote the set of edges of a graph G. If the elements of E(G) are ordered pairs, G is called a directed graph or digraph; Otherwise, G is called an undirected graph In an undirected graph, the pairs (u, v) and (v, u) represent the same edge Data Structures Using C++

Various Undirected Graphs Data Structures Using C++

Various Directed Graphs Data Structures Using C++

Graph Representation: Adjacency Matrix Let G be a graph with n vertices, where n > 0 Let V(G) = {v1, v2, ..., vn} The adjacency matrix AG is a two-dimensional n × n matrix such that the (i, j)th entry of AG is 1 if there is an edge from vi to vj; otherwise, the (i, j)th entry is 0 Data Structures Using C++

Graph Representation: Adjacency Matrix Data Structures Using C++

Graph Representation: Adjacency Lists In adjacency list representation, corresponding to each vertex, v, is a linked list such that each node of the linked list contains the vertex u, such that (v, u)  E(G) Array, A, of size n, such that A[i] is a pointer to the linked list containing the vertices to which vi is adjacent Each node has two components, (vertex and link) Component vertex contains index of vertex adjacent to vertex i Data Structures Using C++

Graph Representation: Adjacency Matrix Data Structures Using C++

Graph Representation: Adjacency Matrix Data Structures Using C++

Data Structures Using C++ Operations on Graphs Create the graph: store in memory using a particular graph representation Clear the graph: make the graph empty Determine whether the graph is empty Traverse the graph Print the graph Data Structures Using C++

class linkedListGraph template<class vType> class linkedListGraph: public linkedListType<vType> { public: void getAdjacentVertices(vType adjacencyList[], int& length); //Function to retrieve the vertices adjacent to a given //vertex. //Postcondition: The vertices adjacent to a given vertex // are retrieved in the array adjacencyList. The parameter length specifies the number // of vertices adjacent to a given vertex. }; Data Structures Using C++

class linkedListGraph template<class vType> void linkedListGraph<vType>::getAdjacentVertices (vType adjacencyList[], int& length) { nodeType<vType> *current; length = 0; current = first; while(current != NULL) adjacencyList[length++] = current->info; current = current->link; } Data Structures Using C++

Data Structures Using C++ Graph Traversals Depth first traversal 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 Data Structures Using C++

Data Structures Using C++ Depth First Traversal Traversal: 0 1 2 4 3 5 6 8 10 7 9 Data Structures Using C++

Breadth First Traversal The general algorithm is: a. for each vertex v in the graph if v is not visited add v to the queue //start the breadth // first search at v b. Mark v as visited c. while the queue is not empty c.1. Remove vertex u from the queue c.2. Retrieve the vertices adjacent to u c.3. for each vertex w that is adjacent to u if w is not visited c.3.1. Add w to the queue c.3.2. Mark w as visited Data Structures Using C++

Breadth First Traversal Data Structures Using C++

Data Structures Using C++ Weighted Graphs Data Structures Using C++

Shortest Path Algorithm Weight of the edge: edges connecting two vertices can be assigned a nonnegative real number Weight of the path P: sum of the weights of all the edges on the path P; Weight of v from u via P Shortest path: path with smallest weight Shortest path algorithm: greedy algorithm developed by Dijkstra Data Structures Using C++

Shortest Path Algorithm Let G be a graph with n vertices, where n > 0. Let V(G) = {v1, v2, ..., vn}. Let W be a two-dimensional n X n matrix such that: Data Structures Using C++

Data Structures Using C++ Shortest Path The general algorithm is: Initialize the array smallestWeight so that smallestWeight[u] = weights[vertex, u] Set smallestWeight[vertex] = 0 Find the vertex, v, that is closest to vertex for which the shortest path has not been determined Mark v as the (next) vertex for which the smallest weight is found 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 the weight of the path to w via v is smaller than its current weight, update the weight of w to the weight of v + the weight of the edge (v, w) Because there are n vertices, repeat steps 3 through 5 n – 1 times Data Structures Using C++

Data Structures Using C++ Shortest Path Data Structures Using C++

Data Structures Using C++ Shortest Path Data Structures Using C++

Data Structures Using C++ Shortest Path Data Structures Using C++

Shortest Path (end result) Data Structures Using C++

Data Structures Using C++ Minimal Spanning Tree This graph represents the airline connections of a company between seven cities (cost factor shown) Data Structures Using C++

Data Structures Using C++ Minimal Spanning Tree Company needs to shut down the maximum number of connections and still be able to fly from one city to another (may not be directly). Data Structures Using C++

Data Structures Using C++ Minimal Spanning Tree (Free) tree T : 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 weight is assigned to the edges in T If T is a weighted tree, the weight of T, denoted by W(T ), is the sum of the weights of all the edges in T Data Structures Using C++

Data Structures Using C++ Minimal Spanning 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), All the vertices of G are in T. Data Structures Using C++

Data Structures Using C++ Minimal Spanning Tree Theorem: A graph G has a spanning tree if and only if G is connected. In order to determine a spanning tree of a graph, the graph must be connected. Let G be a weighted graph. A minimal spanning tree of G is a spanning tree with the minimum weight. Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm Builds tree iteratively by adding edges until minimal spanning tree obtained Start with a source vertex At each iteration, new edge that does not complete a cycle is added to tree Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm General form of Prim’s algorithm (let n = number of vertices in G): 1. Set V(T) = {source} 2. Set E(T) = empty 3. for i = 1 to n 3.1 minWeight = infinity; 3.2 for j = 1 to n if vj is in V(T) for k = 1 to n if vk is not in T and weight[vj][vk] < minWeight { endVertex = vk; edge = (vj, vk); minWeight = weight[vj][vk]; } 3.3 V(T) = V(T)  {endVertex}; 3.4 E(T) = E(T)  {edge}; Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm Data Structures Using C++

Data Structures Using C++ Prim’s Algorithm Data Structures Using C++

Prim’s Algorithm (end result) Data Structures Using C++

Data Structures Using C++ Precedence Graph Nodes represent events, directed edges represent precedences Event 0 must precede 1 and 5, 3 must be preceded by 1, 4, and 7, etc. Data Structures Using C++

Data Structures Using C++ Topological Order Let G be a directed graph and V(G) = {v1, v2, ..., vn}, where n > 0. A topological ordering of V(G) is a linear ordering vi1, vi2, ..., vin of the vertices such that if vij is a predecessor of vik, j ≠ k, 1 <= j <= n, and 1 <= k <= n, then vij precedes vik, that is, j < k in this linear ordering. Data Structures Using C++

Data Structures Using C++ Topological Order Because the graph has no cycles: There exists a vertex u in G such that u has no predecessor. There exists a vertex v in G such that v has no successor. Data Structures Using C++

Breadth First Topological Order Create the array predCount and initialize it so that predCount[i] is the number of predecessors of the vertex vi Initialize the queue, say queue, to all those vertices vk so that predCount[k] is zero. (Because the graph has no cycles, queue is never empty until all nodes have gone through it.) Data Structures Using C++

Breadth First Topological Order 3. while the queue is not empty Remove the front element, u, of the queue Put u in the next available position, say topologicalOrder[topIndex], and increment topIndex For all the immediate successors w of u Decrement the predecessor count of w by 1 if the predecessor count of w is zero, add w to queue Data Structures Using C++

Breadth First Topological Order Data Structures Using C++

Breadth First Topological Order Data Structures Using C++

Breadth First Topological Order Data Structures Using C++

Breadth First Topological Order (end result) Data Structures Using C++

Data Structures Using C++ Chapter Summary Graphs Graphs as ADTs Traversal algorithms Shortest path algorithms Minimal spanning trees Topological sort Data Structures Using C++