Graphs and Shortest Paths Using ADTs and generic programming.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Analysis of Algorithms CS 477/677
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
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.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
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.
CSE 326: Data Structures Lecture #19 Graphs I Alon Halevy Spring Quarter 2001.
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.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –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.
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.
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.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Important Problem Types and Fundamental Data Structures
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
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.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
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.
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.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
CSE332: Data Abstractions Lecture 15: Introduction to Graphs Tyler Robison 2010 Summer 1.
COSC 2007 Data Structures II Chapter 14 Graphs I.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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.
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
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.
1 CSE 332: Graphs Richard Anderson Spring Announcements This week and next week – Graph Algorithms Reading, Monday and Wednesday, Weiss
Introduction to Graphs
Introduction to Graphs
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS223 Advanced Data Structures and Algorithms
Chapter 11 Graphs.
ITEC 2620M Introduction to Data Structures
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs G = (V, E) V are the vertices; E are the edges.
Important Problem Types and Fundamental Data Structures
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Graphs and Shortest Paths Using ADTs and generic programming

Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph consists of a set of vertices V and a set of edges E  V  V Undirected graph:  E consists of unordered pairs: Edge (u, v) is the same as (v, u) Undirected graphs are drawn with nodes for vertices and line segments for edges

Digraphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A directed graph, or digraph:  E is set of ordered pairs, and not necessarily a symmetric set. Even if edge (u, v) is present, the edge (v, u) may be absent Directed graphs are drawn with nodes for vertices and arrows for edges

Paths and Weights A path is a sequence of vertices w 1,w 2,…,w n, where there is an edge for each pair of consecutive vertices The length of a path of n vertices is n-1 (the number of edges) A path is simple if all its vertices are distinct (except that the first and last may be equal) Edges may have weights associated with them The cost of a path is the sum of the weights of the edges along the path v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v

Cycles A cycle is a path w 1,w 2,…,w n =w 1, where the first and the last vertices are the same  A cycle is simple if the path is simple. Above, v 2, v 8, v 6, v 3, v 5, v 2 is a (simple) cycle in the undirected graph, but not (even a path) in the digraph v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4

Further terminology An undirected graph G is connected if, for each pair of vertices u, v, there is a path that starts at u and ends at v A digraph H that satisfies the above condition is strongly connected Otherwise, if H is not strongly connected, but the undirected graph G with the same set of vertices and edges is connected, H is said to be weakly connected v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4

Data structures representing graphs A digraph can be stored as a 2-dimensional array A, with indexes ranging from 0 to n-1, where n=|V| Unweigthed graphs: A[u][v]==true if edge (u,v) is in the graph, otherwise A[u][v]== false Graphs with weights: A[u][v]== w, the weight of the edge (u,v) Undirected graphs can be represented more efficiently, having the second range be 0 to i, where i is the index in the first range. This representation, the Adjacency Matrix, requires O(n 2 ) space, where n is the number of vertices It is wasteful for sparse graphs, where most of the possible edges are not present.

Adjacency matrix example v1v1 v2v2 v5v5 v7v7 v0v0 v3v3 v6v6 v4v4 A FFFFFFTF 1FFFFTFFF 2TFFFFFFF 3FFFFFTTF 4FFFFFFFF 5FFTFFFFF 6FFFFFFFF 7TFFFFFFF

Graph density and efficient storage A complete graph contains an edge for every pair of vertices On the other extreme, sparse graphs contain much fewer than the O(n 2 ) possible edges  Example: Every intersection in Manhattan is a vertex, an edge is the portion of a street between intersections  In this graph, each vertex has an average of 4 edges, so |E| = O(n) The adjacency list is a space- efficient approach to storing sparse graphs For each vertex, maintain a list of outgoing edges (pointers to other vertices) Takes O(|V|+|E|) space  Approach 1: A Hashtable maps vertices to adjacency lists  Approach 2: Vertex structure maintains a list of pointers to other vertices

Adjancency list example v1v1 v2v2 v5v5 v7v7 v0v0 v3v3 v6v6 v4v4 06,

Graph-based algorithms Introduction to some graph-based problems and approaches

Topological sorting Let G be a directed acyclic graph (DAG) Topological sorting refers to ordering the vertices of G such that if there is an edge from v i to v j, then v j appears after v i

Topological sorting E.g.: Binary tree, where the binary nodes store no pointers to parent  Children pointers are viewed as representing directed edges from parent to child The binary node is a vertex structure Level-order and pre-order would be topological sortings in this graph. 1 root

Topological sorting (example) A topological sorting of the above could be:  MAC3311, COP3210, MAD2104, CAP3700, COP3400, COP3337, COP4555, MAD3305, MAD3512, COP3530, CDA4101, COP4610, CDA4400, COP4225, CIS4610, COP5621, COP4540

Topological sorting (algorithm) In a DAG, there must be a vertex with no incoming edges Have each vertex maintain its indegree Have each vertex maintain a Boolean flag found Repeatedly find a vertex of current indegree 0, assign it a rank, then reduce the indegrees of the vertices in its adjacency list If a vertex of current indegree 0 has been already found, a cycle exists, output failure

Initializing indegree computation struct Vertex{ list adj; // Adjacency list bool known; int indegree; int rank; // Other data and // member functions // as needed }; // Assume vertices in // vector verts; void computeInDegrees(){ for(int i=0; i<verts.size(); verts[i].indegree =0, i++); for(int i=0; i< verts.size(); i++) { for(list ::iterator j= verts[i].adj.begin(); j!= verts[i].adj.end(); (*j).indegree++, j++); } In plain English, the above first sets all indegrees temporarily to 0, then visits all vertices’ adjancency lists, and for each edge found, increments the indegree of pointed vertex

Topological sorting (final) int topologicalSort() { list zeroIn; computeInDegrees(); //Initialize indegrees for(int i=0; i< verts.size(); i++) { if (verts[i].indegree ==0) zeroIn.push_back(verts[i]); } int rank=0; Vertex curr; while(rank < verts.size()) { if(zeroIn.empty()) return FAILURE; //A cycle found! curr = zeroIn.back(); zeroIn.pop_back(); curr.rank = rank++; for(list ::iterator j = curr.adj.begin(); j != curr.adj.end(); j++) { (*j).indegree--; if( (*j).indegree == 0) zeroIn.push_back(*j); } return SUCCESS; }