1 CSC 321: Data Structures Fall 2013 Graphs & search  graphs, isomorphism  simple vs. directed vs. weighted  adjacency matrix vs. adjacency lists 

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Graph Theory.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
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 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
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)
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
Graphs CS3240, L. grewe.
Advanced Data Structures
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
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 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 Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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 and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
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++
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.
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.
– Graphs 1 Graph Categories Strong Components Example of Digraph
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Graphs and Paths : Chapter 15 Saurav Karmakar
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
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.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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.
abstract data types built on other ADTs
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
CSC 321: Data Structures Fall 2015 Graphs & search graphs, isomorphism
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
Chapter 11 Graphs.
Data Structures and Analysis (COMP 410)
Important Problem Types and Fundamental Data Structures
Presentation transcript:

1 CSC 321: Data Structures Fall 2013 Graphs & search  graphs, isomorphism  simple vs. directed vs. weighted  adjacency matrix vs. adjacency lists  Java implementations  graph traversals depth-first search, breadth-first search

2 Graphs trees are special instances of the more general data structure: graphs  informally, a graph is a collection of nodes/data elements with connections many real-world and CS-related applications rely on graphs  the connectivity of computers in a network  cities on a map, connected by roads  cities on a airline schedule, connected by flights  Facebook friends  finite automata

Interesting graphs 3

4 graph of the Internet  created at San Diego Supercomputer Center (UCSD)  depicts round-trip times of data packets sent from Herndon, VA to various sites on the Internet

Interesting graphs 5 TouchGraph Facebook Browser  allows you to view all of your friends and groups as a graph

Interesting graphs 6 graph view of  blue : for links  red : for tables  green : for the DIV tag  violet : for images  yellow : for forms  orange : for breaks & blockquotes  black : the HTML tag  gray : all other tags

Interesting graphs 7 NFL week 11  edges from one team to another show a clear beat path  e.g., Chicago > Tampa > Indy

Formal definition a simple graph G consists of a nonempty set V of vertices, and a set E of edges (unordered vertex pairs)  can write simply as G = (V, E) e.g., consider G = (V, E) where V = {a, b, c, d} and E = {{a,b}, {a, c}, {b,c}, {c,d}}  DRAW IT! 8 graph terminology for G = (V, E)  vertices u and v are adjacent if {u, v}  E(i.e., connected by an edge)  edge {u,v} is incident to vertices u and v  the degree of v is the # of edges incident with it (or # of vertices adjacent to it)  |V| = # of vertices|E| = # of edges  a path between v 1 and v n is a sequence of edges from E: [ {v 1, v 2 }, {v 2, v 3 }, …, {v n-2, v n-1 }, {v n-1, v n } ]

Depicting graphs you can draw the same set of vertices & edges many different ways 9 are these different graphs?

Isomorphism how can we tell if two graphs are basically the same?  G 1 = (V 1, E 1 ) and G 2 = (V 2, E 2 ) are isomorphic if there is mapping f:V 1  V 2 such that {u,v}  E 1 iff {f(u), f(v)}  E 2  i.e., G 1 and G 2 are isomorphic if they are identical modulo a renaming of vertices 10

Common graphs  empty graph E n has n vertices and no edges  line graph L n has n vertices connected in sequenceHOW MANY?  complete graph K n has n vertices with an edge between each pair HOW MANY? 11 E5E5 L5L5 K5K5

Simple vs. directed? in a simple graph, edges are presumed to be bidirectional {u,v}  E is equivalent to saying {v,u}  E for many real-world applications, this is appropriate  if computer c 1 is connected to computer c 2, then c 2 is connected to c 1  if person p 1 is Facebook friends with person p 2, then p 2 is friends with p 1 however, in other applications the connection may be 1-way  a flight from city c 1 to city c 2 does not imply a return flight from c 2 to c 1  roads connecting buildings in a town can be 1-way (with no direct return route)  NFL beat paths are clearly directional 12

Directed graphs a directed graph or digraph G consists of a nonempty set V of vertices, and a set E of edges (ordered vertex pairs)  draw edge (u,v) as an arrow pointing from vertex u to vertex v e.g., consider G = (V, E) where V = {a, b, c, d} and E = {(a,b), (a, c), (b,c), (c,d)}  DRAW IT! 13

Weighted graphs a weighted graph G = (V, E) is a graph/digraph with an additional weight function ω:E    draw edge (u,v) labeled with its weight  shortest path from BOS to SFO?  does the fewest legs automatically imply the shortest path in terms of miles? 14 from Algorithm Design by Goodrich & Tamassia

Graph data structures  an adjacency matrix is a 2-D array, indexed by the vertices for graph G, adjMatrix[u][v] = 1 iff {u,v}  E(store weights for digraphs) 15  an adjacency list is an array of linked lists for graph G, adjList[u] contains v iff {u,v}  E(also store weights for digraphs)

Matrix vs. list space tradeoff  adjacency matrix requires O(|V| 2 ) space  adjacency list requires O(|V| + |E|) space which is better? 16  it depends  if the graph is sparse (few edges), then |V|+|E| < |V| 2  adjacency list  if the graph is dense, i.e., |E| is O(|V| 2 ), then |V| 2 < |V|+|E|  adjacency matrix many graph algorithms involve visiting every adjacent neighbor of a node  adjacency list makes this efficient, so tends to be used more in practice

Java implementations to allow for alternative implementations, we will define a Graph interface import java.util.Set; public interface Graph { public void addEdge(E v1, E v2); public Set getAdj(E v); public boolean containsEdge(E v1, E v2); } many other methods are possible, but these suffice for now from this, we can implement simple graphs or directed graphs  can utilize an adjacency matrix or an adjacency list 17

DiGraphMatrix public class DiGraphMatrix implements Graph { private E[] vertices; private Map lookup; private boolean[][] adjMatrix; public DiGraphMatrix(Collection vertices) { this.vertices = (E[])(new Object[vertices.size()]); this.lookup = new HashMap (); int index = 0; for (E nextVertex : vertices) { this.vertices[index] = nextVertex; lookup.put(nextVertex, index); index++; } this.adjMatrix = new boolean[index][index]; } public void addEdge(E v1, E v2) { Integer index1 = this.lookup.get(v1); Integer index2 = this.lookup.get(v2); if (index1 == null || index2 == null) { throw new java.util.NoSuchElementException(); } this.adjMatrix[index1][index2] = true; } to create adjacency matrix, constructor must be given a list of all vertices however, matrix entries are not directly accessible by vertex name  index  vertex requires vertices array  vertex  index requires lookup map

DiGraphMatrix... public boolean containsEdge(E v1, E v2) { Integer index1 = this.lookup.get(v1); Integer index2 = this.lookup.get(v2); return index1 != null && index2 != null && this.adjMatrix[index1][index2]; } public Set getAdj(E v) { int row = this.lookup.get(v); Set neighbors = new HashSet (); for (int c = 0; c < this.adjMatrix.length; c++) { if (this.adjMatrix[row][c]) { neighbors.add(this.vertices[c]); } return neighbors; } 19 containsEdge is easy  lookup indices of vertices  then access row/column getAdj is more work  lookup index of vertex  traverse row  collect all adjacent vertices in a set

DiGraphList public class DiGraphList implements Graph { private Map > adjLists; public DiGraphList() { this.adjLists = new HashMap >(); } public void addEdge(E v1, E v2) { if (!this.adjLists.containsKey(v1)) { this.adjLists.put(v1, new HashSet ()); } this.adjLists.get(v1).add(v2); } public Set getAdj(E v) { if (!this.adjLists.containsKey(v)) { return new HashSet (); } return this.adjLists.get(v); } public boolean containsEdge(E v1, E v2) { return this.adjLists.containsKey(v1) && this.getAdj(v1).contains(v2); } 20 we could implement the adjacency list using an array of LinkedLists  simpler to make use of HashMap to map each vertex to a Set of adjacent vertices (wastes some memory, but easy to code)  note that constructor does not need to know all vertices ahead of time

GraphMatrix & GraphList public class GraphMatrix extends DiGraphMatrix { public GraphMatrix(Collection vertices) { super(vertices); } public void addEdge(E v1, E v2) { super.addEdge(v1, v2); super.addEdge(v2, v1); } 21 constructors simply call the superclass constructors addEdge adds edges in both directions using the superclass addEdge public class GraphList extends DiGraphList { public GraphList() { super(); } public void addEdge(E v1, E v2) { super.addEdge(v1, v2); super.addEdge(v2, v1); } can utilize inheritance to implement simple graph variants  can utilize the same internal structures, just add edges in both directions

Graph traversal many applications involve systematically searching through a graph  starting at some vertex, want to traverse the graph and inspect/process vertices along paths e.g., list all the computers on the network e.g., find a sequence of flights that get the customer from BOS to SFO (perhaps with some property?) e.g., display the football teams in the partial ordering similar to tree traversals, we can follow various patterns  recall for trees: inorder, preorder, postorder for arbitrary graphs, two main alternatives  depth-first search (DFS): boldly follow a single path as long as possible  breadth-first search (BFS): tentatively try all paths level-by-level 22

DFS vs. BFS Consider the following digraph: 23 depth-first search (starting at 0) would  select an adjacent vertex 0  1  select adjacent vertex from there0  1  3  select adjacent vertex from there0  1  3  2  select adjacent vertex from there0  1  3  2  4  … breadth-first search (starting at 0) would  generate length-1 paths0  1 0  4  expand to length-2 paths0  1  3 0  4  1  expand to length-3 paths0  1  3  2 0  4  1  3  …

Bigger examples DFS? BFS? 24

DFS implementation depth-first search can be implemented easily using recursion 25 public static void DFS1(Graph g, E v) { GraphSearch.DFS1(g, v, new HashSet ()); } private static void DFS1(Graph g, E v, Set visited) { if (!visited.contains(v)) { System.out.println(v); visited.add(v); for (E adj : g.getAdj(v)) { GraphSearch.DFS1(g, adj, visited); } here, it just prints the vertices as they are visited – we could process in a variety of ways need to keep track of the visited vertices to avoid cycles  here, pass around a HashSet to remember visited vertices

DFS implementation depth-first search can be implemented easily using recursion 26 public static void DFS1(Graph g, E v) { GraphSearch.DFS1(g, v, new HashSet ()); } private static void DFS1(Graph g, E v, Set visited) { if (!visited.contains(v)) { System.out.println(v); visited.add(v); for (E adj : g.getAdj(v)) { GraphSearch.DFS1(g, adj, visited); } here, it just prints the vertices as they are visited – we could process in a variety of ways need to keep track of the visited vertices to avoid cycles  here, pass around a HashSet to remember visited vertices

Stacks & Queues DFS can alternatively be implemented using a stack  start with the initial vertex on the stack  at each step, visit the vertex at the top of the stack  when you visit a vertex, pop it & push its adjacent neighbors (as long as not already visited) BFS is most naturally implemented using a queue  start with the initial vertex in the queue  at each step, visit the vertex at the front of the queue  when you visit a vertex, remove it & add its adjacent neighbors (as long as not already visited) 0 4  2  1 3  5  4  2 4  3  5  4 1  4  3  5 1  4  3 2  1  4 2 

Implementations public static void DFS2(Graph g, E v) { Stack unvisited = new Stack (); unvisited.push(v); Set visited = new HashSet (); while (!unvisited.isEmpty()) { E nextV = unvisited.pop(); if (!visited.contains(nextV)) { System.out.println(nextV); visited.add(nextV); for (E adj : g.getAdj(nextV)) { unvisited.push(adj); } 28 public static void BFS(Graph g, E v) { Queue unvisited = new LinkedList (); unvisited.add(v); Set visited = new HashSet (); while (!unvisited.isEmpty()) { E nextV = unvisited.remove(); if (!visited.contains(nextV)) { System.out.println(nextV); visited.add(nextV); for (E adj : g.getAdj(nextV)) { unvisited.add(adj); } DFS utilizes a Stack of unvisited vertices  results in the longest path being expanded as before, uses a Set to keep track of visited vertices & avoid cycles BFS is identical except that the unvisited vertices are stored in a Queue  results in the shortest path being expanded similarly uses a Set to avoid cycles

Topological sort a slight variant of DFS can be used to solve an interesting problem  topological sort: given a partial ordering of items (e.g., teams in a beat-path graph), generate a possible complete ordering (e.g., a possible ranking of teams) 29 Consider Bears subgraph only: 1. CHI 2. ATL 3. TB 4. TEN 5. SEA 6. IND 7. SD 8. MIN 9. CAR 10. ARI 11. STL 1. CHI 2. TB 3. SD 4. MIN 5. ATL 6. TEN 7. SEA 8. ARI 9. STL 10. IND 11. CAR

Topological sort 30 public static void DFS1(Graph g, E v) { GraphSearch.DFS1(g, v, new HashSet ()); } private static void DFS1(Graph g, E v, Set visited) { if (!visited.contains(v)) { System.out.println(v); visited.add(v); for (E adj : g.getAdj(v)) { GraphSearch.DFS1(g, adj, visited); } public static void topo(Graph g, E v) { GraphSearch.topo(g, v, new HashSet ()); } private static void topo(Graph g, E v, Set visited) { if (!visited.contains(v)) { visited.add(v); for (E adj : g.getAdj(v)) { GraphSearch.topo(g, adj, visited); } System.out.println(v); } if you take the recursive DFS code and do the print AFTER the recursion, you end up with a topological sort (in reverse)

Finite State Machines many useful problems can be defined using a special weighted graph  a Finite State Machine (a.k.a. Finite Automaton ) defines finite set of states (i.e., vertices) along with transitions between those states (i.e., edges) e.g., the logic controlling a coin-operated turnstile 31 can be in one of two states: locked or unlocked  if locked, pushing  it does not allow passage & stays locked inserting coin  unlocks it  if unlocked,pushing  allows passage & then relocks inserting coin  keeps it unlocked

More complex example e.g., a vending machine that takes coins (N, D, Q) and sells gum for 35¢ (for now, assume exact change only) 32 5¢ 10¢ 20¢ 15¢ 25¢ 0¢ 30¢ Q Q D D D D D D N N N N Q N N N 35¢ 0¢ is the start state (a.k.a. the initial state )  there can only be one 35¢ is a goal state (a.k.a. an accepting state )  there can be multiple ones HOW COULD WE HANDLE RETURN CHANGE?

Recognition/acceptance we say that a FSM recognizes a pattern if all sequences that meet that pattern terminate in an accepting state 33 alternatively, we say that a FSM defines a language made up of the sequences accepted by the FSM  a language is known as a regular language if there exists a FSM that accepts it note: * denotes arbitrary repetition, + denotes OR L 1 = 1*(01*0)*1*L 2 = ( )*

Deterministic vs. nondeterministic the examples so far are deterministic, i.e., each transition is unambiguous  a FSM can also be nondeterministic, if there are multiple transitions from a state with different labels/weights  nondetermineistic FSM are especially useful for defining complex regular languages 34

Uses of FSM FSM machines are useful in many areas of CS  designing software for controlling devices (e.g., vending machine, elevator, …) may start by identifying states, constructing a table of state transitions, finally implementing in code  parsing symbols for programming languages or text processing may define the language using rules or via a regular expression, then build a FSM to parse the characters into tokens & symbols  designing circuitry commonly used to model complex circuitry, design components 35

MFT CS exam – sample question 36

GRE CS exam – sample question 37

GRE CS exam – sample question 38