Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs – Depth First Search ORD DFW SFO LAX
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
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.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Connected Components, Directed graphs, Topological sort COMP171 Fall 2005.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection  Connectivity.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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,
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.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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.
DIRECTED ACYCLIC GRAPHS AND TOPOLOGICAL SORT CS16: Introduction to Data Structures & Algorithms Tuesday, March 10,
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Week 11 - Friday.  What did we talk about last time?  Exam 2 post mortem  Cycle detection in undirected graphs  Topological sort and cycle detection.
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
CS 367 Introduction to Data Structures Lecture 13.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Graph Search Applications, Minimum Spanning Tree
Graphs Chapter 20.
School of Computing Clemson University Fall, 2012
Lecture 11 Graph Algorithms
Fundamentals, Terminology, Traversal, Algorithms
C.Eng 213 Data Structures Graphs Fall Section 3.
Depth-First Search.
CS120 Graphs.
"Learning how to learn is life's most important skill. " - Tony Buzan
Spanning Trees.
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Elementary Graph Algorithms
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
CSCI2100 Data Structures Tutorial
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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:

Week 11 - Wednesday

 What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search

Spellchecker

 We have spent a huge amount of time on trees in this class  Trees have many useful properties  What is the only difference between a tree and a graph?  Cycles  Well, technically a tree is also connected

 Is a graph a tree?  It might be hard to tell  We need to come up with an algorithm for detecting any cycles within the possible tree  What can we use?  Depth First Search!

 Nodes all need some extra information, call it number  Startup 1. Set the number of all nodes to 0 2. Pick an arbitrary node u and run Detect( u, 1 )  Detect( node v, int i ) 1. Set number(v) = i++ 2. For each node u adjacent to v If number(u) is 0 Detect( u, i ) Else Print “Cycle found!”

 Even graphs with unconnected components can have cycles  To be sure that there are no cycles, we need to run the algorithm on every starting node that hasn’t been visited yet

 A directed acyclic graph (DAG) is a directed graph without cycles in it  Well, obviously  These can be used to represent dependencies between tasks  An edge flows from the task that must be completed first to a task that must come after  This is a good model for course sequencing  Especially during advising  A cycle in such a graph would mean there was a circular dependency  By running topological sort, we discover if a directed graph has a cycle, as a side benefit

 A topological sort gives an ordering of the tasks such that all tasks are completed in dependency ordering  In other words, no task is attempted before its prerequisite tasks have been done  There are usually multiple legal topological sorts for a given DAG

 Give a topological sort for the following DAG:  A F I C G K D J E H A A H H E E J J D D K K G G C C F F I I

 Create list L  Add all nodes with no incoming edges into set S  While S is not empty  Remove a node u from S  Add u to L  For each node v with an edge e from u to v ▪ Remove edge e from the graph ▪ If v has no other incoming edges, add v to S  If the graph still has edges  Print "Error! Graph has a cycle"  Otherwise  Return L

 Connected for an undirected graph: There is a path from every node to every other node  How can we determine if a graph is connected?

 Startup 1. Set the number of all nodes to 0 2. Pick an arbitrary node u and run DFS( u, 1 )  DFS( node v, int i ) 1. Set number(v) = i++ 2. For each node u adjacent to v If number(u) is 0 DFS( u, i )  If any node has a number of 0, the graph is not connected

 Connected components are the parts of the graph that are connected to each other  In a connected graph, the whole graph forms a connected component  In a graph that is not entirely connected, how do we find connected components?  DFS again!  We run DFS on every unmarked node and mark all nodes with a number count  Each time DFS completes, we increment count and start DFS on the next unmarked node  All nodes with the same value are in a connected component

 Weakly connected directed graph: If the graph is connected when you make all the edges undirected  Strongly connected directed graph: If for every pair of nodes, there is a path between them in both directions

 Components of a directed graph can be strongly connected  A strongly connected component is a subgraph such that all its nodes are strongly connected  To find strongly connected components, we can use a special DFS  It includes the notion of a predecessor node, which is the lowest numbered node in the DFS that can reach a particular node

 StrongDFS( node v, int i ) 1. Set number(v) = i++ 2. Set predecessor(v) to number(v) 3. Push(v) 4. For each node u adjacent to v If number(u) is 0 StrongDFS( u, i ) predecessor(v) = min(predecessor(v), predecessor(u)) Else if number(u) < number(v) and u is 0n the stack predecessor(v) = min(predecessor(v), number(u)) 5. If predecessor(v) is number(v) w = pop() while w is not v output w w = pop() output w

 StronglyConnected( Vertices V )  Set i = 1  For each node v in V Set number(v) = 0  For each node v in V If number(v) is 0 StrongDFS( v, i ) Remember that i is passed by reference

A B I D F H C E G

 Finish strong connectivity  Minimum spanning trees  Shortest paths

 Finish Project 3  Due this Friday before midnight  Read sections 4.3 and 4.4