Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,

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

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
CS203 Lecture 15.
Nattee Niparnan. Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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
Graph Searching CSE 373 Data Structures Lecture 20.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph & BFS.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
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.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
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.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
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.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs 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.
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++
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs and Paths : Chapter 15 Saurav Karmakar
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
Lecture 7 Graph Traversal
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.
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.
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.
CSC 172 DATA STRUCTURES.
Graphs A New Data Structure
CS212: Data Structures and Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
CC 215 Data Structures Graph Searching
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
CSE 421: Introduction to Algorithms
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
CSE 373 Data Structures Lecture 16
Richard Anderson Autumn 2016 Lecture 5
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Lecture 11 Graph Algorithms
Presentation transcript:

Nattee Niparnan

Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4, 5),(6,7))

Term you should already know  directed, undirected graph  Weighted graph  Bipartite graph  Tree  Spanning tree  Path, simple path  Circuit, simple circuit  Degree

Representing a Graph  Adjacency Matrix  A = |V|x|V| matrix  a xy = 1when there is an edge connecting node x and node y  a xy = 0otherwise

Representing a Graph  Adjacency List  Use a list instead of a matrix  For each vertex, we have a linked list of their neighbor

Representing a Graph  Incidences Matrix  Row represent edge  Column represent node

Depth First Search

Exploring a Maze

Exploring Problem

Depth-First-Search void explore(G, v) // Input: G = (V,E) is a graph; v  V // Output: visited[u] is set to true for all nodes u reachable from v { visited[v] = true previsit(v) for each edge (v,u)  E if not visited[u] explore(u) postvisit(v) }

Example Explore(A)

Extend to Graph Traversal  Traversal is walking in the graph  We might need to visit each component in the graph  Can be done using explore  Do “explore” on all non-visited node  The result is that we will visit every node  What is the difference between just looking into V (the set of vertices?)

Graph Traversal using DFS void dfs(G) { for all v  V visited[v] = false for all v  V if not visited[v] explore(v) }

Complexity Analysis  Each node is visited once  Each edge is visited twice  Why?  O( |V| + |E|)

Another Example

Connected Component

Connectivity in Undirected Graph

Connected Component Problem  Input:  A graph  Output:  Marking in every vertices identify the connected component  Let it be an array ccnum, indexed by vertices

Solution  Define global variable cc  In previsit(v)  ccnum[v] = cc  Before calling each explore  cc++

DFS visiting time

Ordering in Visit void previsit(v) pre[v] = clock++ void postvisit(v) post[v] = clock++

Ordering in Visit The interval for node u is [pre(u),post(u)] The inverval for u,v is either Contained disjointed Never intersect

DFS in Directed Graph

Type of Edge in Directed Graph

Directed Acyclic Graph (DAG)  A directed Graph without a cycle  Has “source”  A node having only “out” edge  Has “sink”  A node having only “in” edge  How can we detect that a graph is a DAG  What should be the property of “source” and “sink” ?

Solution  A directed graph is acyclic if and only if it has no back edge  Sink  Having lowest post number  Source  Having highest post number

Topological Sorting

Linearization of Graph

Linearization One possible linearization B,A,D,C,E,F Order of work that can be done w/o violating the causality constraints

Topological Sorting Problem

Topological Sorting  Do DFS  List node by post number (descending) 1,12 2,9 3,84,5 6,7 10,11

Breadth First Search

Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2

DFS and Length  DFS finds all nodes reachable from the starting node  But it might not be “visited” according to the distance

Ball and Strings We can compute distance by proceeding from “layer” to “layer”

Shortest Path Problem (Undi, Unit)  Input:  A graph, undirected  A starting node S  Output:  A label on every node, giving the distance from S to that node

Breadth-First-Search  Visit node according to its layer procedure bfs(G, s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: visit[u] is set to true for all nodes u reachable from v for each v  V visited[v] = false visited[s] = true Queue Q = [s] //queue containing just s while Q is not empty v = Q.dequeue() previsit(v) visited[v] = true for each edge (v,u)  E if not visited[u] visited[u] = true Q.enqueue(u) postvisit(v)

Distance using BFS procedure shortest_bfs(G, s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. for all v  V dist[v] = -1 dist[s] = 0 Queue Q = [s] //queue containing just s while Q is not empty v = Q.dequeue() for all edges (v,u)  E if dist[u] = -1 dist[u] = dist[v] + 1 Q.enqueue(u) Use dist as visited

DFS by Stack procedure dfs(G, s) //Input: Graph G = (V,E), directed or undirected; vertex s  V //Output: visited[u] is true for any node u reachable from v for each v  V visited[v] = false visited[s] = true Stack S = [s] //queue containing just s while S is not empty v = S.pop() previsit(v) visited[v] = true for each edge (v,u)  E if not visited[u] visited[u] = true S.push(u) postvisit(v)

DFS vs BFS  DFS goes depth first  Trying to go further if possible  Backtrack only when no other possible way to go  Using Stack  BFS goes breadth first  Trying to visit node by the distance from the starting node  Using Queue