GRAPHS.

Slides:



Advertisements
Similar presentations
Review Binary Search Trees Operations on Binary Search Tree
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
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.
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)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
 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.
Graph & BFS.
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.
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.
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 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.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
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 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.
Graphs CS /02/05 Graphs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
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
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
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.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
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.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
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.
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs Chapter 7 Visit for more Learning Resources.
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:

GRAPHS

Definition The Graph Data Structure Drawing of a Graph set V of vertices collection E of edges (pairs of vertices in V) Drawing of a Graph vertex <-> circle/oval edge <-> line connecting the vertex pair

Sample Uses Airports and flights Persons and acquaintance relationships Intersections and streets Computers and connections; the Internet

Graph Example Graph G=(V,E): V={a,b,c,d}, E={(a,b),(b,c),(b,d),(a,d)}

Adjacency Vertices connected by an edge are adjacent An edge e is incident on a vertex v (and vice-versa) if v is an endpoint of e Degree of a vertex v, deg(v) number of edges incident on v if directed graph, indeg(v) and outdeg(v)

Graph Properties Graph G with n vertices and m edges v in G deg(v) = 2m v in G indeg(v) = v in G outdeg(v) = m m is O(n2) m <= n(n-1)/2 if G is undirected m <= n(n-1) if G is directed

Subgraphs Graph H a subgraph of graph G Spanning subgraph vertex set of H is a subset of vertex set of G edge set of H is a subset of edge set of G Spanning subgraph vertex set of H identical to vertex set of G

Paths and Cycles (simple) Path (simple) Cycle sequence of distinct vertices such that consecutive vertices are connected through edges (simple) Cycle same as path except that first and last vertices are identical Directed paths and cycles edge directions matter

Connectivity Connected graph there is a path between any two vertices

Graph Methods Container methods General or Global methods numVertices(),numEdges(),vertices(),edges() Directed Edge methods indegree(v), inadjacentVertices(v), inincidentEdges(v) Update methods adding/removing vertices or edges

General Methods numvertices() -returns the number of vertices in G numedges() - returns the number of edges in G vertices() - return an enumeration of the vertex positions in G edges() - returns an enumeration of the edge positions in G

General Methods Degree (v) - returns the degree of v adjacentvertices(v) - returns an enumeration of the vertices adjacent to v incidentedges(v) - returns an enumeration of the edges incident upon v endvertices(e) - return an array of size 2 storing the end vertices of e

Directed Edges Directededges() - return an enumeration of all directed edges indegree(v) - return the indegree of v outdegree(v) - return the outdegree of v origin (v) - return the origin of directed edge e other functions - inadjacentvertices(), outadjacentvertices(), destination()

Updating Graphs Insertedge(v,w,o) - insert and return an undirected edge between vertices v and w storing the object o at this position Insertvertex(o) - insert and return a new vertex storing the object o at this position removevertex (v) - remove the vertex v and all incident edges removeedge(e) - remove edge e

Implementation of a graph data structure

Graph Implementations Edge List vertices and edges are nodes edge nodes refer to incident vertex nodes Adjacency List same as edge list but vertex nodes also refer to incident edge nodes Adjacency Matrix 2D matrix of vertices; entries are edges

Implementation Examples Illustrate the following graph using the different implementations 4 a b 5 1 3 c d 2

Edge List

Adjacency List

Adjacency Matrix

Implementation Examples- Query Illustrate the following graph using the different implementations 4 a b 8 5 e 1 3 6 c 7 d 2

Comparing Implementations Edge List simplest but most inefficient Adjacency List many vertex operations are O(d) time where d is the degree of the vertex Adjacency Matrix adjacency test is O(1) addition/removal of a vertex: resize 2D array

Graph Traversals Traversal - A systematic procedure for exploring a graph by examining all of its vertices and edges Consistency - rules to determine the order of visits (top-down, alphabetical,etc.) - make the path consistent with the actual graph (don’t change conventions)

Graph Traversals Depth First Search (DFS) Breadth First Search (BFS) visit a starting vertex s recursively visit unvisited adjacent vertices Breadth First Search (BFS) visit starting vertex s visit unvisited vertices adjacent to s, then visit unvisited vertices adjacent to them, and so on … (visit by levels)

DFS Traversal Depth First Search - recursively visit (traverse) unvisited vertices Dead End - reaching a vertex where all the incident edges go to a previously visited vertex Back Track - If a dead-end vertex is reached, go to the previous vertex

Traversal Examples DFS: a,b,c,d,h,e,f,g,l,k,j,i c b d a e g f h i j l

DFS Traversal Path of the DFS traversal - path=a,b,c,d,h - h is a dead end vertex - back track to d,c,b,a and go to another adjacent (unvisited) vertex -> e - path=a,e,f,g,k,j,i - i is a dead end vertex - back track to all the way back to a (also a dead end vertex) finished

BFS Traversal Visit adjacent vertices by levels discovery edges - edges that lead to an unvisited vertex cross edge - edges that lead to a previously visited vertex

Traversal Examples BFS: a,b,e,i,c,f,j,d,g,k,h,l c b d a e g f h i j l

DFS Algorithm Algorithm DFS(v) Input: Vertex v Output: Depends on the application mark v as visited; perform processing for each vertex w adjacent to v do if w is unmarked then DFS(w)

BFS Algorithm Algorithm BFS(s) Input: Starting vertex s Output: Depends on the application // traversal algorithm uses a queue Q // loop terminates when Q is empty

BFS Algorithm continued Q  new Queue() mark s as visited and Q.enqueue(s) while not Q.isEmpty() v  Q.dequeue() perform processing on v for each vertex w adjacent to v do if w is unmarked then mark w as visited and Q.enqueue(w)

Query Let G be a graph whose vertices are the integers 1 through 8 and let the adjacent vertices of each vertex be given by the table below : Vertex Adjacent Vertices 1 (2,3,4) 2 (1,3,4) 3 (1,2,4) 4 (1,2,3,6) 5 (6,7,8) 6 (4,5,7) 7 (5,6,8) 8 (5,7)

Query Assume that in the traversal of G, the adjacent vertices of a given vertex are returned in the same order as they are listed in the above table: a) Draw G b) Give the sequence of vertices of G visited using a DFS traversal order starting at vertex 1 c) Give the sequence of vertices visited using a BFS traversal starting at vertex 1

Weighted Graphs Overview

Weighted Graphs Graph G = (V,E) such that there are weights/costs associated with each edge w((a,b)): cost of edge (a,b) representation: matrix entries <-> costs a 20 35 e b 12 32 c 65 d

Problems on Weighted Graphs Minimum-cost Spanning Tree Given a weighted graph G, determine a spanning tree with minimum total edge cost Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine the shortest paths from v to all other vertices in G Path length: sum of all edges in path

Weighted Graphs-Concerns Minimum Cost Shortest Path

Weighted Graphs Shortest Path - Dijkstra’s Algorithm Minimum Spanning Trees - Kruskal’s Algorithm