GRAPHS Lecture 18 CS2110 – Fall 2014. Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.

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

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
PSU CS Algorithms Analysis and Design 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.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
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.
Breadth-First and Depth-First Search
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
© 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.
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.
Graphs CS3240, L. grewe.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
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.
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 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Using Search in Problem Solving
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.
Tree Searching Breadth First Search Dept First Search.
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.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
GRAPHS Lecture 19 CS2110 – Fall Time to do A4, Recursion Histogram: max: [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08):
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
Breadth First Search and Depth First Search. Greatest problem in Computer Science Has lead to a lot of new ideas and data structures Search engines before.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Recitation 9 Prelim Review.
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Spanning Trees, greedy algorithms
CS202 - Fundamental Structures of Computer Science II
Data Structures and Algorithms for Information Processing
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Spanning Trees.
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
DFS and Shortest Paths Lecture 18 CS2110 – Spring 2014.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Prim’s algorithm for minimum spanning trees
Spanning Trees, greedy algorithms
Presentation transcript:

GRAPHS Lecture 18 CS2110 – Fall 2014

Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum spanning trees –Prim's algorithm –Kruskal's algorithm

Depth-First Search 3 Given a graph and one of it’s nodes u (say node 1 below). We want to “visit” each node reachable from u ONCE (nodes 1, 0, 2, 3, 5) There are many paths to some nodes. How to visit nodes only once, efficiently, and not do extra work?

Depth-First Search 4 boolean[] visited; node u is visited means: visited[u] is true To visit u means to: set visited[u] to true Node v is REACHABLE from node u if there is a path (u, …, v) in which all nodes of the path are unvisited Suppose all nodes are unvisited. Nodes REACHABLE from node 1: 1, 0, 2, 3, 5 Nodes REACHABLE from 4: 4, 5, 6.

Depth-First Search 5 boolean[] visited; node u is visited means: visited[u] is true To visit u means to: set visited[u] to true Node u is REACHABLE from node v if there is a path (u, …, v) in which all nodes of the path are unvisited Red nodes: visited. Blue nodes: unvisited Nodes REACHABLE from 1: 1, 0, 5 Nodes REACHABLE from 4: none

Depth-First Search 6 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes that are REACHABLE from node 1 are 1, 0, 2, 3, Start End

Depth-First Search 7 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes that are REACHABLE from node 1 are 1, 0, 2, 3, visited[u]= true;

Depth-First Search 8 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes to be visited are 0, 2, 3, visited[u]= true; for each edge (u, v) leaving u: if v is unvisited then dfs(v); Have to do dfs on all unvisited neighbors of u

Depth-First Search 9 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 Nodes to be visited are: 0, 2, 3, visited[u]= true; for each edge (u, v) leaving u: if v is unvisited then dfs(v); Suppose the loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3, 5 Depth First!

Depth-First Search 10 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) leaving u: if v is unvisited then dfs(v); Notes: Suppose n nodes are REACHABLE along e edges (in total). What is Worst-case execution? Worst-case space?

Depth-First Search 11 /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) leaving u: if v is unvisited then dfs(v); Example: Use different way (other than array visited) to know whether a node has been visited That’s all there is to the basic dfs. You may have to change it to fit a particular situation. Example: We really haven’t said what data structures are used to implement the graph. If you don’t have this spec and you do something different, it’s probably wrong.

Depth-First Search in an OO fashion 12 Each node of the graph is an Object of class Node public class Node { boolean visited; List neighbors; } /** This node is unvisited. Visit all nodes REACHABLE from this node */ public void dfs() { } visited= true; for (Node n: neighbors) { if (!n.visited) n.dfs() } No need for a parameter. The object is the node

Depth-First Search written iteratively 13 /** Node u is unvisited. Visit all node REACHABLE from u. */ public static void dfs(int u) { Stack s= (u); // Not Java // inv: all nodes that have to be visited are // REACHABLE from some node in s while ( ) { } s is not empty u= s.pop(); // Remove top stack node, put in u if (u has not been visited) { } for each edge (u, v) leaving u: s.push(v); visit u;

Depth-First Search written iteratively 14 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; for each edge (u, v): s.push(v); } Call dfs(1) stack s 1

Depth-First Search written iteratively 15 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; for each edge (u, v): s.push(v); } Call dfs(1) stack s 1 Iteration

Depth-First Search written iteratively 16 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Stack s= (u); while (s is not empty) { u= s.pop(); if (u has not been visited) { visit u; for each edge (u, v): s.push(v); } Call dfs(1) stack s Iteration Yes, 5 is put on stack twice, once for each edge to it. It will be visited only once. Using a stack causes depth-first search

Breadth-First Search 17 /** Node u is unvisited. Visit all node REACHABLE from u. */ public static void dfs(int u) { Queue q= (u); // Not Java // inv: all nodes that have to be visited are // REACHABLE from some node in q while ( ) { } q is not empty u= q.removeFirst(); // Remove first node, put in u if (u has not been visited) { } for each edge (u, v) leaving u: q.append(v); visit u;

Breadth-First Search 18 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Queue q= (u); while (q is not empty) { u= q.popFirst(); if (u has not been visited) { visit u; for each edge (u, v): append(v); } Call dfs(1) Queue q 1

Breadth-First Search 19 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Queue q= (u); while (q is not empty) { u= q.popFirst(); if (u has not been visited) { visit u; for each edge (u, v): append(v); } Call dfs(1) Queue q 1 Iteration

Breadth-First Search 20 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Queue q= (u); while (q is not empty) { u= q.popFirst(); if (u has not been visited) { visit u; for each edge (u, v): append(v); } Call dfs(1) Queue q Iteration

Breadth-First Search 21 /** u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(int u) { Queue q= (u); while (q is not empty) { u= q.popFirst(); if (u has not been visited) { visit u; for each edge (u, v): append(v); } Call dfs(1) Queue q Iteration Breadth first: (1)Node u (2)All nodes 1 edge from u (3)All nodes 2 edges from u (4)All nodes 3 edges from u …