Graphs - II CS 2110, Spring 2016 1. Where did David leave that book? 2.

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
1 Searching in a Graph Jeff Edmonds York University COSC 3101 Lecture 5 Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
Tree Searching. Tree searches A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions,
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graph Searching CSE 373 Data Structures Lecture 20.
CS 261 – Recitation 9 & 10 Graphs & Final review
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
17-Jun-15 Searching in BeeperHunt (Mostly minor variations on old slides)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Tree Searching Breadth First Search Dept First Search.
GRAPHS
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
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,
COSC 2007 Data Structures II Chapter 14 Graphs III.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Prims Algorithm for finding a minimum spanning tree
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Source: David Lee Matuszek
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Graphs Representation, BFS, DFS
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Fundamentals, Terminology, Traversal, Algorithms
Programming Abstractions
Graph Search Lecture 17 CS 2110 Fall 2017.
CS 106B Homework 7: Trailblazer
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Spanning Trees.
Chapter 11 Graphs.
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Tree Searching.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Spanning Tree Algorithms
Tree Searching.
Tree Searching.
Tree Searching.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Prim’s algorithm for minimum spanning trees
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 - II CS 2110, Spring

Where did David leave that book? 2

Where did David leave that book? 3

Where did David leave that book? Go as far down a path as possible before backtracking – Depth-First Search 4

Graph Algorithms Search – Depth-first search – Breadth-first search Shortest paths – Dijkstra's algorithm Minimum spanning trees – Prim's algorithm – Kruskal's algorithm 5

Reachability Node v is reachable from node u if there is a path from u to v Which nodes are reachable from node 1? 6

Reachability Node v is reachable from node u if there is a path from u to v Which nodes are reachable from node 1? 0, 1, 2, 3, 5 7

Reachability Node v is reachable from node u if there is a path from u to v Which nodes are reachable from node 4? 8

Reachability Node v is reachable from node u if there is a path from u to v Which nodes are reachable from node 4? 3, 4, 5, 6 9

Reachability How to determine reachability efficiently? We need an invariant! 10

Reachability Node v is reachable from node u without green nodes if there is a path from u to v without green nodes Which nodes are reachable from node 1 without green nodes? 11

Reachability Node v is reachable from node u without green nodes if there is a path from u to v without green nodes Which nodes are reachable from node 1 without green nodes? 1 12

Reachability Node v is reachable from node u without green nodes if there is a path from u to v without green nodes Which nodes are reachable from node 4 without green nodes? 13

Reachability Node v is reachable from node u without green nodes if there is a path from u to v without green nodes Which nodes are reachable from node 4 without green nodes? None! Node 4 is green, so all paths from node 4 contain a green node! 14

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Start at node 1 Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to some child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to some child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further No new way to extend path, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to a different child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to some child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Already visited, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further No new way to extend path, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further No new way to extend path, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to a different child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Extend path to some child Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Already visited, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further No new way to extend path, so backtrack Which nodes are reachable from node 1?

Depth-First Search Keep pushing the search forward Mark nodes as “visited” (green) as you go Backtrack only when you can’t go any further Nothing to backtrack, so all done! Which nodes are reachable from node 1?

Depth-First Search using Recursion /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { if (u.hasBeenVisited()) return; } Which nodes are reachable from node 4 without green nodes? None! 44 30

Depth-First Search using Recursion /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { if (u.hasBeenVisited()) return; }

Depth-First Search using Recursion /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { if (u.hasBeenVisited()) return; u.visit(); for (Node v with edge from u to v) dfs(v); }

Depth-First Search using Recursion /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { if (u.hasBeenVisited()) return; u.visit(); for (Node v with edge from u to v) dfs(v); }

Depth-First Search using Recursion /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { if (u.hasBeenVisited()) return; u.visit(); for (Node v with edge from u to v) dfs(v); }

OO-style Recursive Depth-First Search 35 class Node { final List targets; // edges go from this to targets boolean visited= false; // has this node been visited? Node(Node… targets) { this.targets= Arrays.asList(targets); } /*Visit all nodes reachable from this without visited nodes*/ void dfs() { if (visited) return; visited= true; for (Node v : targets) v.dfs(); } }

Depth-First Search using Iteration /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { Collection work= new Stack (); work.add(u); // inv: all nodes that have to be visited are // reachable (without visited nodes) from some node in work while ( ) { Node u= work.pop(); // Remove first node and put it in u if ( ) { u.visit(); for (Node v with edge from u to v) work.add(v); // Stack adds nodes to front } } } !work.isEmpty() !u.hasBeenVisited() 36

Breadth-First Search Mark closest nodes as “visited” (green) first Then push search out further Which nodes are reachable from node 1?

Breadth-First Search Mark closest nodes as “visited” (green) first Then push search out further Visit nodes distance 0 from node 1 Which nodes are reachable from node 1?

Breadth-First Search Mark closest nodes as “visited” (green) first Then push search out further Visit nodes distance 1 from node 1 Which nodes are reachable from node 1?

Breadth-First Search Mark closest nodes as “visited” (green) first Then push search out further Visit nodes distance 2 from node 1 Which nodes are reachable from node 1?

Breadth-First Search Mark closest nodes as “visited” (green) first Then push search out further No nodes at distance 3, so all done! Which nodes are reachable from node 1?

Depth-First Search using Iteration /** Visit all nodes reachable from u without visited nodes */ void dfs(Node u) { Collection work= new Stack (); work.add(u); // inv: all nodes that have to be visited are // reachable (without visited nodes) from some node in work while (!work.isEmpty()) { Node u= work.pop(); // Remove first node and put it in u if (!u.hasBeenVisited()) { u.visit(); for (Node v with edge from u to v) work.add(v); // Stack adds nodes to front } 42

Breadth-First Search using Iteration /** Visit all nodes reachable from u without visited nodes */ void bfs(Node u) { Collection work= new Queue (); work.add(u); // inv: all nodes that have to be visited are // reachable (without visited nodes) from some node in work while (!work.isEmpty()) { Node u= work.pop(); // Remove first node and put it in u if (!u.hasBeenVisited()) { u.visit(); for (Node v with edge from u to v) work.add(v); // Queue adds nodes to back } 43