90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
Graph Searching CSE 373 Data Structures Lecture 20.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 27 Graphs and Applications.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
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++
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
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.
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.
Using nextAdjacent() To list all vertices connected to a vertex u in graph G we can use the following loop: for (int v=G.nextAdjacent(u,-1); v>=0; v=G.nextAdjacent(u,v))
Data Structures and Algorithms Graphs Graph Representations PLSD210(ii)
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Hashing with Separate Chaining Implementation – data members public class SCHashTable implements HashTableInterface { private List [] table; private int.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
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 & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graph Implementations Chapter 29 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 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,
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect 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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 30 Graphs and Applications.
GRAPHS. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different implementations.
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.
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++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
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.
Lecture 9: Graphs & Graph Models. Definition of a Graph edge vertex cycle path.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
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.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Csc 2720 Instructor: Zhuojun Duan
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graph Implementation.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures and Algorithms for Information Processing Lecture 7: Graphs

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 2 Lecture 7: Graphs Today’s Topics Graphs –undirected graphs –directed graphs Graph Traversals –depth-first (recursive vs. stack) –breadth-first (queue)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Lecture 7: Graphs Graph Definitions Nodes and links between them May be linked in any pattern (unlike trees) Vertex: a node in the graph Edge: a connection between nodes

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Lecture 7: Graphs Undirected Graphs Vertices drawn with circles Edges drawn with lines V0 V4 V3 V2 V1 Vertices are labelled Edges are labelled e3 e2 e0 e1 e4 e5 Visual Layout Doesn’t Matter!

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 Lecture 7: Graphs Undirected Graphs An undirected graph is a finite set of vertices along with a finite set of edges. The empty graph is an empty set of vertices and an empty set of edges. Each edge connects two vertices The order of the connection is unimportant

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 6 Lecture 7: Graphs Graphing a Search Space The “three coins” game (p. 692) (use green = heads, red = tails) Switch from green, red, green to red, green, red You may flip the middle coin any time End coins can flip only when the other two coins match each other

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 7 Lecture 7: Graphs Graphing a Search Space Start Finish Often, a problem can be represented as a graph, and finding a solution is obtained by performing an operation on the graph (e.g. finding a path)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 8 Lecture 7: Graphs Directed Graphs A directed graph is a finite set of vertices and a finite set of edges; both sets may be empty to represent the empty graph. Each edge connects two vertices, called the source and target; the edge connects the source to the target (order is significant)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 9 Lecture 7: Graphs Directed Graphs Useful for modelling directional phenomena, like geographical travel, or game-playing where moves are irreversible Examples: modelling states in a chess game, or Tic-Tac-Toe Arrows are used to represent the edges; arrows start at the source vertex and point to the target vertex

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 10 Lecture 7: Graphs Graph Terminology Loops: edges that connect a vertex to itself Paths: sequences of vertices p0, p1, … pm such that each adjacent pair of vertices are connected by an edge Multiple Edges: two nodes may be connected by >1 edge

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 Lecture 7: Graphs Graph Terminology Simple Graphs: have no loops and no multiple edges

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 Lecture 7: Graphs Graph Implementations Adjacency Matrix –A square grid of boolean values –If the graph contains N vertices, then the grid contains N rows and N columns –For two vertices numbered I and J, the element at row I and column J is true if there is an edge from I to J, otherwise false

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 Lecture 7: Graphs Adjacency Matrix falsefalse truefalsefalse 1falsefalse falsetruefalse 2falsetruefalsefalsetrue 3falsefalsefalsefalsefalse 4falsefalsefalsetruefalse

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 14 Lecture 7: Graphs Adjacency Matrix Can be implemented with a two- dimensional array, e.g.: boolean[][] adjMatrix; adjMatrix = new boolean[5][5];

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Lecture 7: Graphs Edge Lists Graphs can also be represented by creating a linked list for each vertex null … For each entry J in list number I, there is an edge from I to J.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 16 Lecture 7: Graphs Edge Sets Use a previously-defined set ADT to hold the integers corresponding to target vertices from a given vertex IntSet[] edges = new IntSet[5]; Quiz: Draw a Red Black Tree implementation of IntSet.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Lecture 7: Graphs Worst-Case Analysis Adding or Removing Edges –adjacency matrix: small constant –edge list: O(N) –edge set: O(logN) if B-Tree is used Checking if an Edge is Present –adjacency matrix: small constant –edge list: O(N) –edge set: O(logN) if B-Tree or Red Black tree is used

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18 Lecture 7: Graphs Worst-Case Analysis Iterating through a Vertex’s Edges –adjacency matrix: O(N) –edge list: O(E) if there are E edges –edge set: O(E) if a B-Tree or Red Black tree implementation is used - In a dense graph, E will be at most N.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 19 Lecture 7: Graphs Choice of Implementation Based on: –which operations are more frequent –whether a good set ADT is available –average number of edges per vertex (a matrix is wasteful for sparse graphs, takes (n 2 ) space).

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 20 Lecture 7: Graphs Programming Example Simple, directed, labeled Graph For generality, labels will be represented as references to Java’s Object class

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 21 Lecture 7: Graphs Instance Variables public class Graph { private boolean[][] edges; private Object[] labels; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 22 Lecture 7: Graphs Constructor public Graph (int n) { edges = new boolean[n][n]; labels = new Object[n]; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 23 Lecture 7: Graphs addEdge Method public void addEdge(int s, int t) { edges[s][t] = true; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 24 Lecture 7: Graphs getLabel Method public Object getLabel(int v) { return labels[v]; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 25 Lecture 7: Graphs isEdge Method public boolean isEdge(int s, int t) { return edges[s][t]; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 26 Lecture 7: Graphs neighbors Method public int[] neighbors(int v) { int i; int count; int [] answer; count = 0; for (i=0; i<labels.length; i++) { if (edges[v][i]) count++; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 27 Lecture 7: Graphs neighbors Method (cont.) answer = new int[count]; count = 0; for (I=0; I<labels.length; I++) { if (edges[v][I]) answer[count++] = I; } return answer; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 28 Lecture 7: Graphs removeEdge Method public void removeEdge(int s, int t) { edges[s][t] = false; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 29 Lecture 7: Graphs setLabel, size Methods public void setLabel(int v, Object n) { labels[v] = n; } public int size() { return labels.length; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 30 Lecture 7: Graphs Graph Traversals Start at a particular vertex Find all vertices that can be reached from the start by following every possible path (set of edges) Q: What could go wrong? (Hint: how do graphs differ from trees?) A: We have to be careful about detecting cycles

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 31 Lecture 7: Graphs Graph Traversals Cycle detection can be implemented with an array of boolean values representing “shading” on the vertices Each vertex is shaded once it is visited boolean[] marked; marked = new boolean[g.size()];

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 32 Lecture 7: Graphs Depth-First Traversal Use a stack (or recursion) to store set of vertices to be visited From a given vertex, visit neighbors I+1 … N only after traversing all possible edges from neighbor I

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 33 Lecture 7: Graphs Depth-First Traversal

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 34 Lecture 7: Graphs Breadth-First Traversal Use a queue to store set of vertices to be visited Visit all neighbors before visiting any of their neighbors

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 35 Lecture 7: Graphs Breadth-First Traversal

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 36 Lecture 7: Graphs Depth-First Traversal public static void dfPrint (Graph g, int start) { boolean[] marked = new boolean[g.size()]; dfRecurse(g, start, marked); }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 37 Lecture 7: Graphs Depth-First Example public static void dfRecurse (Graph g, int v, boolean[] marked) { int [] next = g.neighbors(v); int i, nextNeighbor; marked[v] = true; System.out.println(g.getLabel(v)); (continued on next slide)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 38 Lecture 7: Graphs Depth-First Example for (i=0;i<next.length;i++) { nextNeighbor = next[i]; if (!marked[nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked); } }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 39 Lecture 7: Graphs Breadth-First Implementation Uses a queue of vertex numbers The start vertex is processed, marked, placed in the queue Repeat until queue is empty: –remove a vertex v from the queue –for each unmarked neighbor u of v: process u, mark u, place u in the queue