CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Graphs – Depth First Search ORD DFW SFO LAX
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
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.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.
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 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.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
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
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
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.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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.
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.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Chapter 22 Elementary Graph Algorithms
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CSC 413/513: Intro to Algorithms
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015

Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper” in the graph whenever possible ■ Edges are explored out of the most recently discovered vertex v that still has unexplored edges ■ When all of v’s edges have been explored, backtrack to the vertex from which v was discovered Mudasser Naseer 2 12/16/2015

Depth-First Search ● Vertices initially colored white ● Then colored gray when discovered ● Then black when finished Mudasser Naseer 3 12/16/2015

Depth-First Search: The Code Mudasser Naseer 4 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code What does u->d represent? Mudasser Naseer 5 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code What does u->f represent? Mudasser Naseer 6 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code Will all vertices eventually be colored black? Mudasser Naseer 7 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code What will be the running time? Mudasser Naseer 8 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code Running time: O(n 2 ) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times Mudasser Naseer 9 12/16/2015 DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }

Depth-First Search: The Code DFS(G) { 1 for each vertex u  G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u  G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v  u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; } BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called? Mudasser Naseer 10 12/16/2015

Depth-First Search: The Code DFS(G) { for each vertex u  G->V { u->color = WHITE; } time = 0; for each vertex u  G->V { if (u->color == WHITE) DFS_Visit(u); } DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] { if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; time = time+1; u->f = time; } So, running time of DFS = O(V+E) Mudasser Naseer 11 12/16/2015

● What is the running time of DFS? The loops on lines 1–2 and lines 4–6 of DFS take time (V), exclusive of the time to execute the calls to DFS-VISIT. ● The procedure DFSVISIT is called exactly once for each vertex v ∈ V, since DFS-VISIT is invoked only on white vertices and the first thing it does is paint the vertex gray. During an execution of DFS-VISIT(v), the loop on lines 4–6 is executed |Adj[v]| times. Since the total cost of executing lines 4–6 of DFS-VISIT is  (E). The running time of DFS is therefore (V + E). Mudasser Naseer 12 12/16/2015

DFS Example source vertex Mudasser Naseer 13 12/16/2015

DFS Example 1 | | | | | | | | source vertex d f Mudasser Naseer 14 12/16/2015

DFS Example 1 | | | | | | 2 | | source vertex d f Mudasser Naseer 15 12/16/2015

DFS Example 1 | | | | |3 | 2 | | source vertex d f Mudasser Naseer 16 12/16/2015

DFS Example 1 | | | | |3 | 4 2 | | source vertex d f Mudasser Naseer 17 12/16/2015

DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f Mudasser Naseer 18 12/16/2015

DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f Mudasser Naseer 19 12/16/2015

DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f Mudasser Naseer 20 12/16/2015

DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f Mudasser Naseer 21 12/16/2015

DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the grey vertices? What do they represent? Mudasser Naseer 22 12/16/2015

DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 23 12/16/2015

DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 24 12/16/2015

DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 25 12/16/2015

DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 26 12/16/2015

DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 27 12/16/2015

DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 28 12/16/2015

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 29 12/16/2015

DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ○ The tree edges form a spanning forest ○ Can tree edges form cycles? Why or why not? Mudasser Naseer 30 12/16/2015

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edges Mudasser Naseer 31 12/16/2015

DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor ○ Encounter a grey vertex (grey to grey) Mudasser Naseer 32 12/16/2015

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edges Mudasser Naseer 33 12/16/2015

DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor ■ Forward edge: from ancestor to descendent ○ Not a tree edge, though ○ From grey node to black node Mudasser Naseer 34 12/16/2015

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edges Mudasser Naseer 35 12/16/2015

DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor ■ Forward edge: from ancestor to descendent ■ Cross edge: between a tree or subtrees ○ From a grey node to a black node Mudasser Naseer 36 12/16/2015

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges Mudasser Naseer 37 12/16/2015

DFS: Kinds of edges ● DFS introduces an important distinction among edges in the original graph: ■ Tree edge: encounter new (white) vertex ■ Back edge: from descendent to ancestor ■ Forward edge: from ancestor to descendent ■ Cross edge: between a tree or subtrees ● Note: tree & back edges are important; most algorithms don’t distinguish forward & cross Mudasser Naseer 38 12/16/2015

DFS And Graph Cycles ● Thm: An undirected graph is acyclic iff a DFS yields no back edges ■ If acyclic, no back edges (because a back edge implies a cycle ■ If no back edges, acyclic ○ No back edges implies only tree edges (Why?) ○ Only tree edges implies we have a tree or a forest ○ Which by definition is acyclic ● Thus, can run DFS to find whether a graph has a cycle Mudasser Naseer 39 12/16/2015

DFS And Cycles ● What will be the running time? ● A: O(V+E) ● We can actually determine if cycles exist in O(V) time: ■ In an undirected acyclic forest, |E|  |V| - 1 ■ So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way Mudasser Naseer 40 12/16/2015

Example: DFS of Undirected Graph A B C D E F G G=(V,E) Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B

Example: DFS of Undirected Graph Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E A B C D E F G T T B TBB T T B T B

Example: (continued) DFS of Undirected Graph DFS Tree A B C D E F G T T B TBB T T B T B A C D E F G B B T T T T T T B B B B

Using DFS & BFS A directed graph G is acyclic if and only if a Depth- First Search of G yields no back edges. Using DFS to Detect Cycles: Using BFS for Shortest Paths: A Breadth-First Search of G yields shortest path information: For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.

Directed Acyclic Graphs A directed acyclic graph or DAG is a directed graph with no directed cycles:

Directed Acyclic Graphs (DAGs) DAG - digraph with no cycles compare: tree, DAG, digraph with cycle DE CB A DE CB A DE CB A

Where DAGs are used Syntactic structure of arithmetic expressions with common sub-expressions e.g.((a+b)*c + ((a+b)+e)*(e+f)) * ((a+b)*c) * + * * ++ + ab c ef

Where DAGs are used? To represent partial orders A partial order R on a set S is a binary relation such that –for all a in Sa R a is false (irreflexive) –for all a, b, c in Sif a R b and b R c then a R c (transitive) examples: “less than” (<) and proper containment on sets S ={1, 2, 3} P(S) - power set of S (set of all subsets) {1, 2, 3} {1, 2}{1, 3}{2, 3} {1}{2}{3} { }

DAGs in use To model course prerequisites or dependent tasks Year 1Year 2Year 3Year 4 Compiler construction Prog. Languages DS&APUMA Data & Prog. Discrete Math Data Comm 1 Data Comm 2 Op Systems Real time systems Distributed Systems

DFS and DAGs Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges: –=> if G is acyclic, will be no back edges Trivial: a back edge implies a cycle –<= if no back edges, G is acyclic Proof by contradiction: G has a cycle   a back edge –Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle –When v discovered, whole cycle is white –Must visit everything reachable from v before returning from DFS-Visit() –So path from u  v is grey  grey, thus (u, v) is a back edge