Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,

Slides:



Advertisements
Similar presentations
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
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.
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.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
CS202 - Fundamental Structures of Computer Science II
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.
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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved 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.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
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.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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,
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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.
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.
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
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.
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 & Algorithm Analysis lec(8):Graph T. Souad alonazi
Csc 2720 Instructor: Zhuojun Duan
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Chapter 13.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 9: Graphs Shortest Paths
GRAPH – Definitions A graph G = (V, E) consists of
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:

Graphs

Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system, computer network. Ex. Mass transportation system, computer network.

Terminology  A graph consists of a set of nodes (vertices) and a set of edges.  An edge connects two distinct nodes (vertices).  A node is basic component, which usually contains some information.  A subgraph is graph which consists of a subset of nodes (vertices) and a subset of edges of a graph.

 A path is a sequence of nodes such that each successive pair is connected by an edge.  A path is a simple path if each of its nodes occurs once in the sequence.  A cycle is path that is a simple path except that the first and last nodes are the same. A path Simple path cycle

 A graph is a connected graph if there is a path between every pair of its nodes. Connected graph Not a connected graph

 Two nodes are adjacent nodes if there is an edge that connects them.  Neighbors of a node are all nodes that are adjacent to it.  A Tree is the special case of a graph that (i) is connected (ii) has no cycle

 If a connected graph has n nodes and n-1 edges, then it is a tree. This tree is called Spanning Tree.  An oriented tree is a tree in which one node has been designated the root node.

 A directed graph or digraph is a graph in which each edge has an associated direction.  A weighted graph is a graph in which each edge has an associated value

Specification of Graph Elements: A graph consists of nodes and edges Structure: An edge is a one-to-one relationship between a pair of distinct nodes. A pair of nodes can be connected by at most one edge, but any node can be connected to any collection of other nodes. Domain: The number of nodes (vertices) in a graph is bounded. Operations: InsertNode ( Type e) Requires: G is not full. Results: If G does not contain an element whose key value is e.key then e is inserted in G and inserted is true, otherwise inserted is false.

InsertEdge (Key k1, k2) Requires: G is not full and k1!=k2. Results: If G contains two nodes whose key values are k1 and k2 then G contains an edge connecting those nodes. If the two nodes were connected by an edge before operation InsertEdge then inserted is false; otherwise inserted is true. DeleteNode (Key k) Results: G does not contain an element whose key value is k. if G contained a node before this operation with key value k then deleted is true and no edge that connected this node to an other node is in G; otherwise deleted is false. DeleteEdge (Graph g, Key k1, k2) Results: G does not contain an edge that connects nodes whose key values are k1 and k2. If G contained such an edge before this operation then deleted is true; otherwise deleted is false.

Update (T e) Results: If G contained a node with key value e.key then the element in the node is e and updated is true. Otherwise updated is false. Retrieve (key k, T e) Results: If G contains a node whose key value is k before this operation then e is that element and retrieved is true; otherwise retrieved is false. Full () Results: If G is full then Full returns true; otherwise Full returns false.

Representation of a Graph  There are two approaches to representing graphs (i) Adjacency Matrix (ii) Adjacency List

 This representation is very simple, but the space requirement is O(n 2 ) if the number of vertices is n.  This representation is suitable only if the graph is dense with the number of edges i.e. O(n 2 ), n being the number of vertices. But in most of the applications this is not true. Adjacency matrix A two dimensional array whose components are of type Boolean and whose index values correspond to the nodes EX If A is the adjacency matrix corresponding to a graph G, then A ij corresponds to the edge (i, j), and is true if there is an edge between the vertices i, and j.

Adjacency list It is an array where each cell corresponds to a vertex of a graph and stores the header of a list of all adjacent vertices NULL Ex. The following is an adjacency list corresponding to the graph on the right.  This is an ideal representation if the graph is not dense.  In this case the space requirement is O(e + n) where e is the number of edges and n is the number of vertices.

Traversal of a Graph  There are two methods for graph traversal (i) Breadth First Search (ii) Depth First Search Process each node of the graph only once

Breadth First Search (BFS) Start at some source node s and visit all its neighbors, then visit the neighbors of each neighbor of s and so on. 1.Assign the status of waiting to all nodes of a graph. 2.Start with source node s, and put it in queue 3.Dequeue one node from the queue, assign it the status of processed, and assign its neighbors the status of ready and put them in the queue. 4.Repeat Step 3 until the queue is empty. ALgorithm

Example Consider a graph with nodes a, b, c, d, e, f, g, s

Pseudocode for BFS BFS(G, s) { for each node u of G set status[u] = waiting; status[s] = ready; Q = {s}; while( Q.IsEmpty) while( Q.IsEmpty){ u = Q.dequeue(); for each v in Neigh[u] { if(status[v] = waiting) { staus[v] = ready; staus[v] = ready; Q.enqueue(v); Q.enqueue(v);}} status[u] = processed; }}

Depth First Search (DFS) DFS visits one node and then visits one of its neighbors and puts the rest of its neighbors into a stack and so on. 1.Assign the status of waiting to all nodes of a graph. 2.For each node of the graph, follow the following steps, a)Change the status of the node to be ready and put in a stack b)Repeat the following steps until the stack is empty c)Get a node from the stack, process it, change its status to processed and change the status from waiting to ready of its neighbors and put them in the stack d)Go to step b

a s d b c e f g S: s a s d b c e f g S: a c d s a s d b c e f g S: a c b d a s d c e f g S: a c g b b

a s d c e f g b b a s d c e f g S: a c f e g b a s d c e f g S: e f c a b

Pseudocode for DFS BFS(G){ for each node u of G status[u] = waiting; for each node u of G if(status[u] == waiting) Visit(u)}Visit(u){ stack S; status[u] = ready; S.push(u) while( S.IsEmpty) while( S.IsEmpty){ v = S.pop(); for each w in Neigh[v] { if(status[w] = waiting) { staus[w] = ready; staus[w] = ready; S.push(w); S.push(w);}} status[v] = processed; }}

Shortest Path (unweighted)  The problem: Find the shortest path from a vertex v to every other vertex in a graph G  The unweighted path measures the number of edges, ignoring the edge’s weights (if any)

Finding Shortest Unweighted Path (Simple Algorithm) 1Mark all vertices with d v = infinity 2Select a starting vertex s, and set d s = 0, and set shortest = 0 3For all vertices v with d v = shortest, scan their adjacency lists for vertices w where d w is infinity –For each such vertex w, set d w to shortest+1 4Increment shortest and repeat step 3, until there are no vertices w For a vertex v, d v is the distance between a starting vertex and v

Simple algorithm: example v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v6 0       v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v6 0 1   1   v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v   v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v

Simple algorithm: using queues  In step 3 of the simple algorithm, we search through the entire graph for vertices v with d v = shortest  Instead, because we deal with exactly these vertices in the previous iteration of step 3, we can queue them v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v6 0 1   1   Queue: v 0, v 5 v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v   Queue: v 5, v 1, v 3

Shortest weighted path: Dijkstra’s (Dike-Struhalgorithm Shortest weighted path: Dijkstra’s (Dike-Struh) algorithm  What is the lowest-cost path between two vertices in a weighted graph?  Only works on graphs all of whose weights are positive  Works in a similar way to the unweighted algorithm, except we possibly adjust the values in the vertices more than once

Dijkstra’s (Dike-Struh Algorithm Dijkstra’s (Dike-Struh) Algorithm NOTE:  We can use an ‘inverse’ priority queue, in order to select the vertex with lowest cost  Algorithm based on breadth-first traversal Initialise all costs to infinity Select a start vertex current_v = start vertex Repeat For every adjacent vertex v of current_v For every adjacent vertex v of current_v If (cost of current_v + weight of edge to v) < cost of v If (cost of current_v + weight of edge to v) < cost of v Reduce cost of v to (cost of current_v + weight of edge v) Reduce cost of v to (cost of current_v + weight of edge v) Select lowest cost vertex as current_v and examine adjacent vertices Select lowest cost vertex as current_v and examine adjacent vertices Until all vertices have been processed

Example v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v6  0     Queue: v 0 v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v6    Queue: v 3, v 1

v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: v 1, v 4, v 2, v 6, v 5 v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: v 4, v 2, v 6, v 5

v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: v 2, v 6, v 5 v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: v 6, v 5

v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: v 5 v2v2 v3v3 v4v4 v0v0 v1v1 v5v5 v6v Queue: The shortest path from v 0 to v 6 is v 0 v 3 v 6 and the shortest path from v 0 to v 4 is v 0 v 3 v 4

Complexity of Dijkstra’s algorithm The time complexity is O(|V| 2 + |E|) For dense graphs where |E| = O(|V| 2 ), the algorithm is optimal and the time complexity is O(|V| 2 ) For sparse graphs, it can be improved to give a time complexity of O(|E| log |V|)