Graphs - Definition G(V,E) - graph with vertex set V and edge set E

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Cs420dl lecture 7 Graph traversals wim bohm cs csu.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Algorithms.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Topological Sort (an application of DFS)
CS200: Algorithm Analysis
Graph: representation and traversal CISC4080, Computer Algorithms
Binhai Zhu Computer Science Department, Montana State University
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
BFS,DFS Topological Sort
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Topological Sort (an application of DFS)
Elementary Graph Algorithms
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Graphs - Definition G(V,E) - graph with vertex set V and edge set E E  {(a,b)| aV and bV} - for directed graphs E  {{a,b}| aV and bV} - for undirected graphs w: E  R - weight function |V| - number of vertices |E| - number of edges Often we will assume that V = {1, ,n}

Graphs - Examples 6 1 6 1 2 2 3 4 5 3 4 5

Graphs - Trees 6 1 6 1 2 4 2 4 3 5 3 5

Graphs - Directed Acyclic Graphs (DAG) 6 1 2 4 3 5

Graphs - Representations - Adjacency matrix 1 2 3 4 5 6 6 1 2 3 4 5 6 1 1 2 3 4 5

Graphs - Representations - Adjacency lists 6 1 1 2 3 4 5 6 6 2 6 2 3 3 4 5 1 3 5 2 1

Breadth-First Search - Algorithm BreadthFirstSearch(graph G, vertex s) for u  V[G]  {s} do colour[u]  white; d[u] ; p[u]  0 colour[s]  gray; d[s]  0; p[s]  0 Q  {s} while Q  0 do u  Head[Q] for v  Adj[u] do if colour[v] = white then colour[v]  gray; d[v]  d[u] + 1; p[v]  u EnQueue(Q,v) DeQueue(Q) colour[u]  black

Breadth-First Search - Example g

Breadth-First Search - Example        d e f g s Q

Breadth-First Search - Example 1    1   d e f g e a Q

Breadth-First Search - Example 1 2   1 2  d e f g a b f Q

Breadth-First Search - Example 1 2  2 1 2  d e f g b f d Q

Breadth-First Search - Example 1 2 3 2 1 2  d e f g f d c Q

Breadth-First Search - Example 1 2 3 2 1 2 3 d e f g d c g Q

Breadth-First Search - Example 1 2 3 2 1 2 3 d e f g c g Q

Breadth-First Search - Example 1 2 3 2 1 2 3 d e f g g Q

Breadth-First Search - Example 1 2 3 2 1 2 3 d e f g Q = 

Breadth-First Search - Complexity BreadthFirstSearch(graph G, vertex s) for u  V[G]  {s} do colour[u]  white; d[u] ; p[u]  0 colour[s]  gray; d[s]  0; p[s]  0 Q  {s} while Q  0 do u  Head[Q] for v  Adj[u] do if colour[v] = white then colour[v]  gray; d[v]  d[u] + 1; p[v]  u EnQueue(Q,v) DeQueue(Q) colour[u]  black (V) Thus T(V,E)=(V+E) (V) without for cycle (E) for all while cycles together

Breadth-First Search - Shortest Distances Theorem After BreadthFirstSearch algorithm terminates d[v] is equal with shortest distance from s to v for all vertices v for all vertices v reachable from s the one of the shortest paths from s to v contains edge (p[v], v)

Depth-First Search - Algorithm DepthFirstSearch(graph G) for u  V[G] do colour[u]  white p[u]  0 time  0 if colour[v] = white then DFSVisit(v)

Depth-First Search - Algorithm DFSVisit(vertex u) time  time + 1 d[u]  time colour[u]  gray for v  Adj[u] do if colour[v] = white then p[v]  u DFSVisit(v) colour[u]  black f[u]  time

Depth-First Search - Example b c d e

Depth-First Search - Example b 1/ c d e

Depth-First Search - Example b 1/ 2/ c d e

Depth-First Search - Example b 1/ 2/ 3/ c d e

Depth-First Search - Example b 1/ 2/ 4/ 3/ c d e

Depth-First Search - Example b 1/ 2/ B 4/ 3/ c d e

Depth-First Search - Example b 1/ 2/ B 4/5 3/ c d e

Depth-First Search - Example b 1/ 2/ B 4/5 3/6 c d e

Depth-First Search - Example b 1/ 2/7 B 4/5 3/6 c d e

Depth-First Search - Example b 1/ 2/7 B F 4/5 3/6 c d e

Depth-First Search - Example b 1/8 2/7 B F 4/5 3/6 c d e

Depth-First Search - Example b 1/8 2/7 9/ B F 4/5 3/6 c d e

Depth-First Search - Example b 1/8 2/7 9/ B C F 4/5 3/6 c d e

Depth-First Search - Example b 1/8 2/7 9/ B C F 4/5 3/6 10/ c d e

Depth-First Search - Example b 1/8 2/7 9/ B C F B 4/5 3/6 10/ c d e

Depth-First Search - Example b 1/8 2/7 9/ B C F B 4/5 3/6 10/11 c d e

Depth-First Search - Example b 1/8 2/7 9/12 B C F B 4/5 3/6 10/11 c d e

Depth-First Search - Complexity DepthFirstSearch(graph G) for u  V[G] do colour[u]  white p[u]  0 time  0 if colour[v] = white then DFSVisit(v) (V) executed (V) times DFSVisit(vertex u) time  time + 1 d[u]  time colour[u]  gray for v  Adj[u] do if colour[v] = white then p[v]  u DFSVisit(v) colour[u]  black f[u]  time (E) for all DFSVisit calls together Thus T(V,E)=(V+E)

Depth-First Search - Classification of Edges Trees edges - edges in depth-first forest Back edges - edges (u, v) connecting vertex u to an v in a depth-first tree (including self-loops) Forward edges - edges (u, v) connecting vertex u to a descendant v in a depth-first tree Cross edges - all other edges

Depth-First Search - Classification of Edges Theorem In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

Depth-First Search - White Path Theorem If during depth-first search a “white” vertex u is reachable from a “grey” vertex v via path that contains only “white” vertices, then vertex u will be a descendant on v in depth-first search forest.

Depth-First Search - Timestamps Parenthesis Theorem After DepthFirstSearch algorithm terminates for any two vertices u and v exactly one from the following three conditions holds the intervals [d[u],f[u]] and [d[v],f[v]] are entirely disjoint the intervals [d[u],f[u]] is contained entirely within the interval [d[v],f[v]] and u is a descendant of v in depth- first tree the intervals [d[v],f[v]] is contained entirely within the interval [d[u],f[u]] and v is a descendant of u in depth-

Depth-First Search - Timestamps b s c 3/6 2/9 1/10 11/16 B F C B 4/5 7/8 12/13 14/15 C C C d e f g

Depth-First Search - Timestamps b f g e a d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (s (b (a (d d) a) (e e) b) s) (c (f f) (g g) c)

Depth-First Search - Timestamps B F b f g C a e C B C d

DFS - Checking for cycles [Adapted from M.Golin]

DFS - Checking for cycles [Adapted from M.Golin]

DFS - Checking for cycles [Adapted from M.Golin]

DFS - Checking for cycles [Adapted from M.Golin]

DFS - Topological Sorting undershorts socks watch pants shoes belt shirt tie jacket

DFS - Topological Sorting [Adapted from M.Golin]

DFS - Topological Sorting [Adapted from M.Golin]

DFS - Topological Sorting TopologicalSort(graph G) call DFS(G) to compute f[v] for all vertices v as f[v] for vertex v is computed, insert onto the front of a linked list return the linked list of vertices

DFS - Topological Sorting - Example 1 undershorts 11/16 socks 17/18 watch 9/10 pants 12/15 shoes 13/14 belt shirt 6/7 1/8 tie 2/5 jacket 3/4

DFS - Topological Sorting - Example 1 socks undershorts pants shoes watch 17/18 11/16 12/15 13/14 9/10 shirt belt tie jacket 1/8 6/7 2/5 3/4

DFS - Topological Sorting - Example 2 [Adapted from M.Golin]

DFS - Topological Sorting Theorem TopologicalSort(G) produces a topological sort of a directed acyclic graph G.

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components [Adapted from L.Joskowicz]

DFS - Strongly Connected Components [Adapted from L.Joskowicz]

DFS - Strongly Connected Components [Adapted from L.Joskowicz]

DFS - Strongly Connected Components [Adapted from L.Joskowicz]

DFS - Strongly Connected Components StronglyConnectedComponents(graph G) call DFS(G) to compute f[v] for all vertices v compute GT call DFS(GT) consider vertices in order of decreasing of f[v] output the vertices of each tree in the depth-first forest as a separate strongly connected component

DFS - Strongly Connected Components 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - Strongly Connected Components 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - Strongly Connected Components 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - SCC - Correctness 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - SCC - Correctness y' y x C(x) Assume that y preceded by y' is the closest vertex to x outside C(x). Then: - d(y)<f(y)<d(x)<d(y) (otherwise we will have xy (in G). - for all x'C(x): d(x)<d(x')<f(x')<f(x) (the largest value of f(x) will have the vertex first "discovered" in C(x)). - thus we have d(y)<f(y)<d(y')<f(y'), however there is and edge (y,y') in G, implying f(y)<d(y') d(y')<y(y). Contradiction.

DFS - SCC - Correctness Lemma If two vertices are in the same strongly connected, then no path between them leaves this strongly connected component. Theorem In any depth-first search, all vertices in the same strongly connected component are placed in the same depth-first tree.

DFS - SCC - Correctness Theorem In a directed graph G = (V,E) the forefather (u) of any vertex uV in any depth-first search of G is an ancestor of u. Corollary In any depth-first search of a directed graph G = (V,E) for all uV vertices u and (u) lie in the same strongly connected component.

DFS - SCC - Correctness Theorem In a directed graph G = (V,E) two vertices u,vV lie in the same strongly connected component if and only if they have the same forefather in a depth-first search of G. StronglyConnectedComponents(G) correctly computes the strongly connected components of a directed graph G.

DFS - SCC - Correctness 2 [Adapted from S.Whitesides]

DFS - SCC - Correctness 2 [Adapted from S.Whitesides]

DFS - SCC - Applications [Adapted from L.Joskowicz]