PSUCS322 HM 1 Languages and Compiler Design II Strongly Connected Components Herbert G. Mayer PSU Spring 2010 rev.: 5/28/2010.

Slides:



Advertisements
Similar presentations
Two algorithms for checking emptiness. How to check for emptiness? Is L (A) = ; ? Need to check if there exists an accepting computation (passes through.
Advertisements

Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
1 SSA review Each definition has a unique name Each use refers to a single definition The compiler inserts  -functions at points where different control.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 More Control Flow John Cavazos University.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
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 267: Automated Verification Lecture 10: Nested Depth First Search, Counter- Example Generation Revisited, Bit-State Hashing, On-The-Fly Model Checking.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 CS 410 Mastery in Programming Chapter 6 Graphs, Focus: Strongly Connected Components Herbert G. Mayer, PSU CS status 7/25/2011.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Lecture 2 Emery Berger University of.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Data Structures DFS, Topological Sort Dana Shapira.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
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.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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,
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graph Theory By: Maciej Kicinski Chiu Ming Luk. Extract Triple words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper.
5. Biconnected Components of A Graph If one city’s airport is closed by bad weather, can you still fly between any other pair of cities? If one computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CS 163 Data Structures Chapter 8 Graphs, Strongly Connected Components Herbert G. Mayer, PSU Status 5/21/2015.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Graph Search Applications, Minimum Spanning Tree
Graphs and Graph Algorithms
CS 201 Compiler Construction
Csc 2720 Instructor: Zhuojun Duan
Efficiently Computing SSA
Depth-First Search.
Graph Algorithms Using Depth First Search
CS 201 Compiler Construction
Graph Search Applications
Optimizing Compilers CISC 673 Spring 2009 More Control Flow
Search Related Algorithms
Subgraphs, Connected Components, Spanning Trees
Strongly Connected Components
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Depth-First Search CSE 2011 Winter April 2019.
Jiří Vyskočil, Radek Mařík 2013
OPERATOR GRAMMAR No Ɛ-transition. No two adjacent non-terminals. Eg.
Presentation transcript:

PSUCS322 HM 1 Languages and Compiler Design II Strongly Connected Components Herbert G. Mayer PSU Spring 2010 rev.: 5/28/2010

PSUCS322 HM 2 Agenda SCC Definition Tarjan’s Algorithm 3-Node Sample Modified Algorithms 4-Node Sample Sample Graph References

PSUCS322 HM 3 SCC Definition Given a directed graph G = { V, E }, with a set of vertices V, and connecting edges E A strongly connected component SCC is a subgraph of G such that any vertex in SCC can reach every vertex in SCC –Not necessarily in 1 step By definition then, a singleton node is an SCC, yet for the detection of loops this is not of interest –A singleton node with a back edge to itself is of interest An SCC does not require a single entry point, a single exit point, or a single back-edge

PSUCS322 HM 4 Tarjan’s Algorithm Given graph G = ( V, E ) num = 0 // unique node number > 0 stack = null // ptr to node stack SCC = 0 // number of SCCs in G // cfg nodes have following fields: // num: a unique number to ID node // lowlink: ties node to others in SCC // pred: ptr to stack predecessor // instack: true if node is in stack forall nodes v in V do if v.num = 0 then // yet unvisited find_scc( v ) end if end forall push( v ) // stack may be null v.pred = stack v.instack = true stack = v end push pop( v ) // val param v is stack copy stack = v.pred v.pred = null v.instack = false end pop find_scc( v ) v.num = v.lowlink = ++num push( v ) forall nodes w in succ( v ) do if w.num = 0 then // not yet visited find_scc( w ) v.lowlink = min( v.lowlink, w.lowlink ) elsif w.instack then v.lowlink = min( v.lowlink, w.num ) end if end forall if v.lowlink = v.num then // v: head of SCC SCC++ // track how many SCCs found print( “SCC %d holds:”, SCC ) repeat x = pop( stack ) print( x.num, “, “ ) until x = v end if end find_scc

PSUCS322 HM 5 3-Node Sample 3 Num: Low: Stack: 2 Num: Low: Stack: 1 Num: Low: Stack:

PSUCS322 HM 6 3-Node Sample, Cont’d Outside call to find_scc( for node 1 ) –Increments num to 1, sets fields.num and.lowlink = 1 –Pushes node 1, i.e., stack = 1, node 1’s predecessor = null –Recursive call to find_scc( for node 2 ) –When done, find that lowlink = num, hence SCC Recursive call find_scc( node 2 ) –Set node 2’s.num and.lowlink = 2 –Stack points to node 2, node 2.pred is node 1 –Has 2 successors: node 1 and node 3 –But node 1 is visited, while node 3 causes new find_scc( node 3 ) Recursive call find_scc( node 3 ) –Set.num and.lowlink = 3 –Stack points to node 3, node 3.pred is node 2 –Has no successor –But lowlink = num hence is SCC, but is a singleton-node SCC

PSUCS322 HM 7 Modified Algorithm When a node v’s lowlink = number, then v is head of an SCC All nodes x are popped of the stack, until node x = v If this happens at the first pop, then Tarjan’s method identifies a singleton node Check beforehand and detect singleton SCCs

PSUCS322 HM 8 Modified Algorithm, Cont’d Given graph G = ( V, E ) num = 0 // unique node number > 0 stack = null // ptr to node stack SCC = 0 // number of SCCs in G // cfg nodes have following fields: // num: a unique number to ID node // lowlink: ties node to others in SCC // pred: ptr to stack predecessor // instack: true if node is in stack forall nodes v in V do if v.num = 0 then // yet unvisited find_scc( v ) end if end forall push( v ) // stack may be null v.pred = stack v.instack = true stack = v end push pop( v ) // val param v is stack copy stack = v.pred v.pred = null v.instack = false end pop find_scc( v ) v.num = v.lowlink = ++num push( v ) forall nodes w in succ( v ) do if w.num = 0 then // not yet visited find_scc( w ) v.lowlink = min( v.lowlink, w.lowlink ) elsif w.instack then v.lowlink = min( v.lowlink, w.num ) end if end forall if v.lowlink = v.num then // v: head of SCC if v = stack then // singleton SCC x = pop( stack ) else SCC++ // track how many SCCs found print( “SCC %d holds:”, SCC ) repeat x = pop( stack ) print( x.num, “, “ ) until x = v end if end find_scc

PSUCS322 HM 9 4-Node Sample 2 Num: Low: Stack: 4 Num: Low: Stack: 3 Num: Low: Stack: 1 Num: Low: Stack:

PSUCS322 HM 10 4-Node Sample, Cont’d Outside call to find_scc( node 1 ) –Increments global num to 1, sets fields v.num and v.lowlink = num = 1 –Pushes node 1, i.e., stack = 1, and first node v.pred = null, but v.instrack - true –Calls find_scc( with node 2 ) –When done, find that lowlink = num = 1, hence SCC, but is top of stack node, so do not record as singleton SCC Recursive call from 1 to find_scc( node 2 ) –Set.num and.lowlink = 2 –Stack points to node 2, node 2.pred is pointer to node 1 –Has 2 successors: node 3 and node 4, both with num = 0 –When back from 3 and 4, lowlink stays 2 since is min() already Recursive call from 2 find_scc( node 3 ) –Set.num and.lowlink = 3 –Stack points to node 3, Recursive call find_scc( node 2 ) –Node 2 has lowlink = 2, hence 3’s lowlink set to 2 Recursive call from 2 find_scc( node 4 ) –Set.num and.lowlink = 4 –Stack points to node 4 –No successor node –Is singleton SCC, but not recorded –return to recursive call for node 2

PSUCS322 HM 11 Sample Graph

PSUCS322 HM 12 References 1.Robert E. Tarjan, Depth-first search and linear graph algorithms, SIAM Journal on Computing, Volume 1, Number 2, pages , Alfred V. Aho et al., Compilers Principles, Techniques, and Tools, Addison-Wesley Publishing Co., ©1986 or newer, ISBN