Chapter 28 Graphs and Applications. Objectives

Similar documents
Chapter 28. Graphs and Applications. Part1. Non-weighted Graphs

GRAPHS AND APPLICATIONS

To model real-world problems using graphs and explain the Seven Bridges of Königsberg problem

Graph Traversals. CS200 - Graphs 1

1 Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS302 - Data Structures using C++

CS 310 Advanced Data Structures and Algorithms

Weighted Graphs and Applications. CSE260, Computer Science B: Honors Stony Brook University

CS 491 CAP Introduction to Graphs and Search

Konigsberg Bridge Problem

Copyright 2000, Kevin Wayne 1

Outline. Graphs. Divide and Conquer.

Graph: representation and traversal

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Graphs & Digraphs Tuesday, November 06, 2007

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

CSI 604 Elementary Graph Algorithms

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Graph Representation

Lecture 9 Graph Traversal

Plan. CMPSCI 311: Introduction to Algorithms. Recall. Adjacency List Representation. DFS Descriptions. BFS Description

GRAPHS Lecture 17 CS2110 Spring 2014

Graph Theory. Many problems are mapped to graphs. Problems. traffic VLSI circuits social network communication networks web pages relationship

TIE Graph algorithms

csci 210: Data Structures Graph Traversals

CSE 100: GRAPH ALGORITHMS

Basic Graph Definitions

Chapter 9. Priority Queue

Practical Session No. 12 Graphs, BFS, DFS Topological Sort

Data Structures and Algorithms. Chapter 7. Graphs

W4231: Analysis of Algorithms

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Engineering Software Development in Java

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Representations of Graphs

Topic 12: Elementary Graph Algorithms

CS 206 Introduction to Computer Science II

To represent weighted edges using adjacency matrices and adjacency lists ( 25.2).

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Basic Graph Algorithms

TIE Graph algorithms

(Re)Introduction to Graphs and Some Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Chapter 22 Elementary Graph Algorithms

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Outlines: Graphs Part-2

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Goals! CSE 417: Algorithms and Computational Complexity!

Graph: representation and traversal

Algorithm Design and Analysis

Introduction to Graphs. CS2110, Spring 2011 Cornell University


Lecture 3: Graphs and flows

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Sample Solutions to Homework #4

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Elementary Graph Algorithms CSE 6331

Introduction to Graphs. common/notes/ppt/

Basic Graph Algorithms (CLRS B.4-B.5, )

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

CSE 100: GRAPH SEARCH

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

COT 6405 Introduction to Theory of Algorithms

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Graph Algorithms. Definition

Today s Outline. CSE 326: Data Structures. Topic #15: Cool Graphs n Pretty Pictures. A Great Mathematician. The Bridges of Königsberg

Graph Algorithms Using Depth First Search

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Algorithms: Lecture 10. Chalmers University of Technology

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Graph implementations :

GRAPHS Lecture 19 CS2110 Spring 2013

22.3 Depth First Search


Info 2950, Lecture 16

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

CSE 417: Algorithms and Computational Complexity. 3.1 Basic Definitions and Applications. Goals. Chapter 3. Winter 2012 Graphs and Graph Algorithms

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Inf 2B: Graphs, BFS, DFS

Module 11: Additional Topics Graph Theory and Applications

Algorithm Design and Analysis

CS24 Week 8 Lecture 1

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Searching in Graphs (cut points)

CS Algorithms and Complexity

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Transcription:

Chapter 28 Graphs and Applications CS2: Data Structures and Algorithms Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox, Wim Bohm, and Russ Wakefield 1 Objectives To model real-world problems using graphs and explain the Seven Bridges of Königsberg problem ( 28.1). To describe the graph terminologies: vertices, edges, simple graphs, weighted/unweighted graphs, and directed/undirected graphs ( 28.2). To represent vertices and edges using lists, edge arrays, edge objects, adjacency matrices, and adjacency lists ( 28.3). To model graphs using the Graph interface, the AbstractGraph class, and the UnweightedGraph class ( 28.4). To display graphs visually ( 28.5). To represent the traversal of a graph using the AbstractGraph.Tree class ( 28.6). To design and implement depth-first search ( 28.7). To solve the connected-circle problem using depth-first search ( 28.8). To design and implement breadth-first search ( 28.9). To solve the nine-tail problem using breadth-first search ( 28.10). 2

Modeling Using Graphs 3 Seven Bridges of Königsberg A C Island 1 D Island 2 B 4

Basic Graph Terminologies What is a graph? G=(V, E) = vertices (nodes) and edges Weighted vs. Unweighted graphs Directed vs. Undirected graphs Adjacent vertices share an edge (Adjacent edges) A vertex is Incident to an edge that it joins Degree of a vertex = number of edges it joins Neighborhood = subgraph with all adjacent vertices A Loop is an edge that connects a vertex to itself A Cycle is a path from a vertex to itself via other vertices 5 Weighted vs Unweighted Graph https://www.slideshare.net/emersonferr/20-intro-graphs 6

Directed vs Undirected Graph https://msdn.microsoft.com/en-us/library/ms379574(v=vs.80).aspx 7 More Graph Terminology Parallel edge: two edges that share the same vertices, also called multiple edges: Multiple edges in red, loops in blue https://en.wikipedia.org/wiki/multigraph 8

More Graph Terminology Simple graph: undirected, unweighted, no loops, no parallel edges: http://mathworld.wolfram.com/simplegraph.html 9 More Graph Terminology Complete graph: simple graph where every pair of vertices are connected by an edge: https://en.wikipedia.org/wiki/complete_graph 10

Representing Graphs Representing Vertices Representing Edges: Edge Array Representing Edges: Edge Objects Representing Edges: Adjacency Matrices Representing Edges: Adjacency Lists 11 Representing Vertices (Nodes) String[] vertices = { Seattle, San Francisco, Los Angles, }; List<String> vertices; or public class City { String name; } City[] vertices = {city0, city1, }; 12

Representing Edges (Arcs) int[][] edges = {{0, 1}, {0, 3} {0, 5}, {1, 0}, {1, 2}, }; or public class Edge { } int u, v; public Edge(int u, int v) { this.u = u; this.v = v; List<Edge> list = new ArrayList<>(); list.add(new Edge(0, 1)); list.add(new Edge(0, 3)); 13 Representing Edges: Adjacency Matrix int[][] adjacencymatrix = { {0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0}, // Seattle {1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // San Francisco {0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Los Angeles {1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, // Denver {0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0}, // Kansas City {1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0}, // Chicago {0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, // Boston {0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0}, // New York {0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1}, // Atlanta {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, // Miami {0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1}, // Dallas {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0} // Houston }; 14

Representing Edges: Adjacency Vertex List List<Integer>[] neighbors = new List[12]; Seattle San Francisco Los Angeles Denver Kansas City Chicago Boston New York Atlanta Miami Dallas Houston neighbors[0] neighbors[1] neighbors[2] neighbors[3] neighbors[4] neighbors[5] neighbors[6] neighbors[7] neighbors[8] neighbors[9] neighbors[10] neighbors[11] 1 3 5 0 2 3 1 3 4 10 0 1 2 4 5 2 3 5 7 8 10 0 3 4 6 7 5 7 4 5 6 8 4 7 9 10 11 8 11 2 4 8 11 8 9 10 List<List<Integer>> neighbors = new ArrayList<>(); 15 Representing Edges: Adjacency Edge List List<Edge>[] neighbors = new List[12]; Seattle San Francisco Los Angeles Denver Kansas City Chicago Boston New York Atlanta Miami Dallas Houston neighbors[0] neighbors[1] neighbors[2] neighbors[3] neighbors[4] neighbors[5] neighbors[6] neighbors[7] Edge(0, 1) Edge(1, 0) Edge(2, 1) Edge(3, 0) Edge(4, 2) Edge(5, 0) Edge(6, 5) Edge(7, 4) Edge(0, 3) Edge(1, 2) Edge(2, 3) Edge(3, 1) Edge(4, 3) Edge(5, 3) Edge(6, 7) Edge(7, 5) Edge(0, 5) Edge(1, 3) Edge(2, 4) Edge(3, 2) Edge(4, 5) Edge(5, 4) Edge(7, 6) Edge(2, 10) Edge(3, 4) Edge(4, 7) Edge(5, 6) Edge(7, 8) Edge(3, 5) Edge(4, 8) Edge(5, 7) Edge(4, 10) neighbors[8] neighbors[9] Edge(8, 4) Edge(9, 8) Edge(8, 7) Edge(9, 11) Edge(8, 9) Edge(8, 10) Edge(8, 11) neighbors[10] Edge(10, 2) Edge(10, 4) Edge(10, 8) Edge(10, 11) neighbors[11] Edge(11, 8) Edge(11, 9) Edge(11, 10) 16

Representing Adjacency Edge List Using ArrayList List<ArrayList<Edge>> neighbors = new ArrayList<>(); neighbors.add(new ArrayList<Edge>()); neighbors.get(0).add(new Edge(0, 1)); neighbors.get(0).add(new Edge(0, 3)); neighbors.get(0).add(new Edge(0, 5)); neighbors.add(new ArrayList<Edge>()); neighbors.get(1).add(new Edge(1, 0)); neighbors.get(1).add(new Edge(1, 2)); neighbors.get(1).add(new Edge(1, 3));...... neighbors.get(11).add(new Edge(11, 8)); neighbors.get(11).add(new Edge(11, 9)); neighbors.get(11).add(new Edge(11, 10)); 17 «interface» Graph<V> +getsize(): int +getvertices(): List<V> +getvertex(index: int): V +getindex(v: V): int +getneighbors(index: int): List<Integer> +getdegree(index: int): int +printedges(): void +clear(): void +addvertex(v: V): boolean +addedge(u: int, v: int): boolean +addedge(e: Edge): boolean +remove(v: V): boolean +remove(u: int, v: int): boolean +dfs(v: int): UnWeightedGraph<V>.SearchTree +bfs(v: int): UnWeightedGraph<V>.SearchTree The generic type V is the type for vertices. Returns the number of vertices in the graph. Returns the vertices in the graph. Returns the vertex object for the specified vertex index. Returns the index for the specified vertex. Returns the neighbors of vertex with the specified index. Returns the degree for a specified vertex index. Prints the edges. Clears the graph. Returns true if v is added to the graph. Returns false if v is already in the graph. Adds an edge from u to v to the graph throws IllegalArgumentException if u or v is invalid. Returns true if the edge is added and false if (u, v) is already in the graph. Adds an edge into the adjacency edge list. Removes a vertex from the graph. Removes an edge from the graph. Obtains a depth-first search tree starting from v. Obtains a breadth-first search tree starting from v. Graph UnweightedGraph<V> #vertices: List<V> #neighbors: List<List<Edge>> +UnweightedGraph() +UnweightedGraph(vertices: V[], edges: int[][]) +UnweightedGraph(vertices: List<V>, edges: List<Edge>) +UnweightedGraph(edges: int[][], numberofvertices: int) +UnweightedGraph(edges: List<Edge>, numberofvertices: int) Vertices in the graph. Neighbors for each vertex in the graph. Constructs an empty graph. Constructs a graph with the specified edges and vertices stored in arrays. Constructs a graph with the specified edges and vertices stored in lists. Constructs a graph with the specified edges in an array and the integer vertices 1, 2,... Constructs a graph with the specified edges in a list and the integer vertices 1, 2,. UnweightedGraph TestGraph Run 18

Graph Spanning Tree from V V V 19 Graph Traversals Depth-first search and breadth-first search Both traversals result in a spanning tree, which can be modeled using a class. UnweightedGraph<V>.SearchTree -root: int -parent: int[] -searchorder: List<Integer> +SearchTree(root: int, parent: int[], searchorder: List<Integer>) +getroot(): int +getsearchorder(): List<Integer> +getparent(index: int): int +getnumberofverticesfound(): int +getpath(index: int): List<V> +printpath(index: int): void +printtree(): void The root of the tree. The parents of the vertices. The orders for traversing the vertices. Constructs a tree with the specified root, parent, and searchorder. Returns the root of the tree. Returns the order of vertices searched. Returns the parent for the specified vertex index. Returns the number of vertices searched. Returns a list of vertices from the specified vertex index to the root. Displays a path from the root to the specified vertex. Displays tree with the root and all edges. 20

Depth-First Search The depth-first search of a graph builds a spanning tree of all of the reachable edges from the starting vertex v, using marking of the visited nodes: Input: G = (V, E) and a starting vertex v Output: a DFS tree rooted at v Tree dfs(vertex v) { visit v; for each neighbor w of v if (w has not been visited) { set v as the parent for w; } dfs(w); } 21 Depth-First Search Example 22

Depth-First Search Example Seattle 807 1331 2097 1003 Chicago 787 983 Boston 214 New York San Francisco 1267 1015 Denver 599 533 Kansas City 1260 888 381 1663 864 Los Angeles 1435 496 781 Atlanta 810 Dallas 239 661 Houston 1187 Miami 23 Applications of the DFS Detecting whether a graph is connected. Search the graph starting from any vertex. If the number of vertices searched is the same as the number of vertices in the graph, the graph is connected. Otherwise, the graph is not connected. Detecting whether there is a path between two vertices. Finding a path between two vertices. Finding all connected components. A connected component is a maximal connected subgraph in which every pair of vertices are connected by a path. Detecting whether there is a cycle in the graph, and finding a cycle in the graph. 24

DFS: Depth First Search Explores edges from the most recently discovered node; backtracks when reaching a dead-end. The book does not used white, grey, black, but uses visited (and implicitly unexplored). Recursive DFS(u): code: mark u as visited for each edge (u,v) : if v is not marked visited : DFS(v) DFS and cyclic graphs When DFS visits a node for the first time it is white. There are two ways DFS can revisit a node: 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then? 26

There are two ways DFS can revisit a node: DFS and cyclic graphs 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then? 27 DFS and cyclic graphs There are two ways DFS can revisit a node: 1. DFS has already fully explored the node. What color does it have then? Is there a cycle then? No, the node is revisited from outside. 2. DFS is still exploring this node. What color does it have in this case? Is there a cycle then? Yes, the node is revisited on a path containing the node itself. So DFS with the white, grey, black coloring scheme detects a cycle when a GREY node is visited 28

Cycle detection: DFS + coloring When a grey (frontier) node is visited, a cycle is detected. 29 Recursive / node coloring version DFS(u): #c: color, p: parent c[u]=grey forall v in Adj(u): if c[v]==white: parent[v]=u DFS(v) c[u]=black

Breadth-First Search The breadth-first traversal of a graph is like the breadthfirst traversal of a tree discussed in 25.2.3, Tree Traversal. With breadth-first traversal of a tree, the nodes are visited level by level. First the root is visited, then all the children of the root, then the grandchildren of the root from left to right, and so on. 31 Breadth-First Search Input: G = (V, E) and a starting vertex v, Output: a BFS tree rooted at v bfs(vertex v) { create an empty queue for storing vertices to be visited; add v into the queue; mark v visited; while the queue is not empty { dequeue a vertex, say u, from the queue process u; for each neighbor w of u if w has not been visited { add w into the queue; set u as the parent for w; mark w visited; } } } 32

Breadth-First Search Example Queue: 0 Queue: 1 2 3 Queue: 2 3 4 isvisited[0] = true isvisited[1] = true, isvisited[2] = true, isvisited[3] = true isvisited[4] = true 33 Breadth-First Search Example Seattle 807 1331 2097 1003 Chicago 787 983 Boston 214 New York San Francisco 1267 1015 Denver 599 533 Kansas City 1260 888 381 1663 864 Los Angeles 1435 496 781 Atlanta 810 Dallas 239 661 Houston 1187 Miami 34

Applications of the BFS Detecting whether a graph is connected. A graph is connected if there is a path between any two vertices in the graph. Detecting whether there is a path between two vertices. Finding a shortest path between two vertices. You can prove that the path between the root and any node in the BFS tree is the shortest path between the root and the node. Finding all connected components. A connected component is a maximal connected subgraph in which every pair of vertices are connected by a path. 35 Graphs Describing Precedence Edge from x to y indicates x should come before y, e.g.: prerequisites for a set of courses dependences between programs set of tasks, e.g. building a computer CS200 - Graphs 36

Graphs Describing Precedence Batman images are from the book Introduction to bioinformatics algorithms CS200 - Graphs 37 Graphs Describing Precedence Want an ordering of the vertices of the graph that respects the precedence relation Example: An ordering of CS courses The graph must not contain cycles. WHY? CS200 - Graphs 38

CS Courses Required for CS and ACT Majors CS163 CS165 CS220 CS270 CS253 CS320 CT310 CS314 CT320 CS356 CS370 CS440 CS464 CS410 Topological Sorting of DAGs DAG: Directed Acyclic Graph Topological sort: listing of nodes such that if (a,b) is an edge, a appears before b in the list Is a topological sort unique? Question: Is a topological sort unique? CS200 - Graphs 40

A directed graph without cycles a b c d e f g a,g,d,b,e,c,f a,b,g,d,e,f,c CS200 - Graphs 41 Topological Sort Algorithm Modification of DFS: Traverse tree using DFS starting from all nodes that have no predecessor. Add a node to the list when ready to backtrack. CS200 - Graphs 42

Topological Sort Algorithm List topposort(graph thegraph) // use stack stck and list lst // push all roots for (all vertices v in the graph thegraph) if (v has no predecessors) stck.push(v) Mark v as visited // DFS while (!stck.isempty()) if (all vertices adjacent to the vertex on top of the stack have been visited) v = stck.pop() lst.add(0, v) else Select an unvisited vertex u adjacent to vertex v on top of the stack stck.push(u) Mark u as visited Set v as parent of u return lst CS200 - Graphs 43 Algorithm 2: Example 1 fc e d bg a a b c d e f g a b g d e f c CS200 - Graphs 44

Topological sorting solution 1/18 10/15 11/14 A B C 2/17 D E 9/16 F 12/13 3/8 G H 6/7 4/5 I Red edges represent spanning tree CS200 - Graphs 45