midterm 2 review and minimum spanning trees
play

Midterm 2 Review and Minimum Spanning Trees Tyler Moore CSE 3353, - PDF document

Notes Midterm 2 Review and Minimum Spanning Trees Tyler Moore CSE 3353, SMU, Dallas, TX March 28, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design


  1. Notes Midterm 2 Review and Minimum Spanning Trees Tyler Moore CSE 3353, SMU, Dallas, TX March 28, 2013 Portions of these slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see http://www.cs.sunysb.edu/~skiena/ Administrivia Notes Midterm 2 next Tuesday, April 2 Covers Graph Algorithms and Recurrence Relations All material through Tuesday March 26 (including Kosaraju’s SCC algorithm, two vertex coloring algorithm, but NOT weighted algorithms) You are allowed notes on one side of a 3x5 index card Review today I will be traveling at a conference next week, so no normal office hours Come see me with any questions this week (extra office hours today 4-5pm and this Friday 1-2pm) Next Fall I’m teaching CSE 5338 Security Economics T/Th 11am-12:20pm Counts as upper-division CS elective, also can be taken as 7338 for 4+1 students and applied to M.S. in Security Engineering Learn to apply microeconomics and data analytics to security problems No security or economics background required Course webpage: http://lyle.smu.edu/~tylerm/courses/econsec/ 2 / 37 HW2 review: Q1d and Q1e Notes There are several data structures you can use to represent a graph’s structure when doing traversal We primarily studied dictionaries mapping a node to its children To identify paths, it is better to use a dictionary mapping a node to its parents 3 / 37 DFS traversal: constructing a parent-child dictionary Notes def dfs web graph ( u r l ) : G, S , Q = {} , s e t () , [ ] # V i s i t e d − s e t and stack Q. append ( s ) # We plan on v i s i t i n g s while Q: # Planned nodes l e f t ? u = Q. pop () # Get one i f u in S : continue # Already v i s i t e d ? Skip i t S . add (u) # We ’ ve v i s i t e d i t now G[ u]= getLinks (u) Q. extend (G[ u ] ) # Schedule a l l neighbors return G 4 / 37

  2. DFS traversal: constructing a child-parent dictionary Notes def dfs web parent s ( u r l ) : P, Q = { u r l : None } , [ u r l ] # Parents and FIFO queue while Q: u = Q. pop () # Constant − time f o r deque for v in getLinks (u ) : i f v in P: continue # Already has parent P[ v ] = u # Reached from u : u i s parent Q. append ( v ) return P 5 / 37 Q1d: Find shortest path between 2 URLs Notes def f i n d s h o r t e s t p a t h ( url1 , u r l 2 ) : #s t a r t with a BFS t r a v e r s a l to obtain c hild − parent mapping P = bfs web parent s ( u r l 1 ) u = u r l 2 #make sure the URL e x i s t s i f u r l 2 not in P: return ”No path between URLs e x i s t s ” #b u i l d the path in r e v e r s e from the d e s t i n a t i o n u r l 2 path = [ u ] while P[ u ] != u r l 1 : i f P[ u ] i s None : #g i v e up i f we f i n d the root return ”No path between URLs e x i s t s ” path . append (P[ u ] ) u = P[ u ] path . append ( u r l 1 ) #don ’ t f o r g e t to add the source path . r e v e r s e () #r e o r d e r the path to s t a r t from u r l 1 return path 6 / 37 Q1d: Find longest shortest path for a URL Notes def find max depth ( u r l ) : P = bfs web parent s ( u r l ) maxpath =[] #keep track of the l o n g e s t path seen so f a r for us in P. keys ( ) : #check a l l URLs re achable from u r l u = us path = [ u ] #b u i l d a path from us to u r l while P[ u ] i s not None : #loop t i l we f i n d the root path . append (P[ u ] ) u = P[ u ] i f l e n ( path) > l e n ( maxpath ) : #i s the new path l o n g e r than c u r r e n t max? path . r e v e r s e () #r e v e r s e our new m a x l i s t maxpath = l i s t ( path ) #r e p l a c e maxpath return maxpath 7 / 37 Coding expectations for the Midterm Notes You should be able to understand written code You should be prepared to write a small amount of code Note: you will not be asked to write any code that is directly lifted from the slides, so please don’t fill up your notecard with all the code you’ve seen on slides Let’s go through one example question 8 / 37

  3. Strongly connected components Notes A strongly connected component is the maximal subset of a graph with a directed path between any two vertices B e g f a b h c d i A C 9 / 37 Strongly connected components Notes B g e B f a b h A C Are supernodes in a DAG? c d i A C 10 / 37 Strongly connected components Notes What if we transpose all edges? B g e B f a b h A C Supernodes still in DAG c d i A C SCCs don’t change 10 / 37 Kosaraju’s algorithm for finding SCCs Notes 1 Get a topological sort of all vertices 2 Transpose the graph (reverse all edges) 3 Traverse the graph in topologically sorted order, adding an SCC each time a dead end is reached. 11 / 37

  4. Kosaraju’s Algorithm for Finding Strongly Connected Notes Components 1. Get a topological sort of all vertices g e topsort: [a, b, e, f, g, c, d, h, i] seen: f a b h {} sccs: [] c d i 12 / 37 Kosaraju’s Algorithm for Finding Strongly Connected Notes Components 2. Reverse all edges g e topsort: [a, b, e, f, g, c, d, h, i] seen: f a b h {} sccs: [] c d i 12 / 37 Kosaraju’s Algorithm for Finding Strongly Connected Notes Components 3. Traverse the graph in topologically sorted order, adding an SCC each time a dead end is reached. g e topsort: [a, b, e, f, g, c, d, h, i] seen: f a b h { a,b,c,d } sccs: [ { a,b,c,d } ] c d i 1st SCC 12 / 37 Kosaraju’s Algorithm for Finding Strongly Connected Notes Components 3. Traverse the graph in topologically sorted order, adding an SCC each time a dead end is reached. 2nd SCC g e topsort: [a, b, e, f, g, c, d, h, i] seen: f a b h { a,b,c,d,e,g,f } sccs: [ { a,b,c,d } , { e,g,f } ] c d i 1st SCC 12 / 37

  5. Kosaraju’s Algorithm for Finding Strongly Connected Notes Components 3. Traverse the graph in topologically sorted order, adding an SCC each time a dead end is reached. 2nd SCC g e topsort: [a, b, e, f, g, c, d, h, i] seen: f a b h { a,b,c,d,e,g,f,h,i } sccs: [ { a,b,c,d } , { e,g,f } , { h,i } ] c d i 1st SCC 3rd SCC 12 / 37 Code for Kosaraju’s SCC Algorithm Notes def t r (G) : # Transpose ( rev . edges of ) G GT = {} for u in G: GT[ u ] = s e t () # Get a l l the nodes in there for u in G: for v in G[ u ] : GT[ v ] . add (u) # Add a l l r e v e r s e edges return GT def scc (G) : GT = t r (G) # Get the transposed graph sccs , seen = [ ] , s e t () for u in d f s t o p s o r t (G) : # DFS s t a r t i n g p o i n t s i f u in seen : continue # Ignore covered nodes C = walk (GT, u , seen ) # Don ’ t go ”backward” ( seen ) seen . update (C) # We ’ ve now seen C sccs . append (C) # Another SCC found print sccs 13 / 37 Exercise: Apply Kosaraju’s SCC Algorithm Notes Graph G Transpose( G ) a g a g d d e e b h b h c c f i f i What is the topological sort of G ? Let’s make the DFS tree What are the strongly connected components? 14 / 37 Vertex-coloring problem Notes 15 / 37

  6. Vertex-coloring problem Notes The vertex-coloring problem seeks to assign a label (aka color) to each vertex of a graph such that no edge links any two vertices of the same color Trivial solution: assign each vertex a different color However, goal is usually to use as few colors as possible Next Thursday, Prof. Matula will give a guest lecture where he proves that any planar graph can be colored by at most 4 distinct colors. 16 / 37 Applications of the vertex-coloring problem Notes Apart from working at National Geographic, when might you encounter a vertex-coloring problem? Vertex-coloring problems arise in scheduling problems, where access to shared resources must be coordinated Example: register allocation by compilers Variables are used for fixed timespan (after initialization, before final use) Two variables with intersecting lifespans can’t be put in the same register We can build a graph with variables assigned to vertices and edges drawn between vertices if the variables’ lifespan intersects Color the graph, and assign variables to the same register if their vertices have the same color 17 / 37 Vertex-coloring problem special case: two colors Notes A bipartite graph is a graph whose vertices U V can be divided into disjoint sets U and V such that every edge connects a vertex in U to one in V . Bipartite graphs arise in matching problems : matching workers to jobs, matching kidney donors with recipients, finding heterosexual mates If we can color a graph’s vertices using just two colors, then we have a bipartite graph Problem: given a graph, find its two-coloring or report that a two-coloring is not possible 18 / 37 Two-coloring algorithm Notes 1 Suppose there are two colors: blue and red. 2 Color the first vertex blue. 3 Do a breadth-first traversal. For each newly-discovered node, color it the opposite of the parent (i.e., red if parent is blue) 4 If the child node has already been discovered, check if the colors are the same as the parent. If so, then the graph isn’t bipartite. 5 If the traversal completes without any conflicting colors, then the graph is bipartite. 19 / 37

Recommend


More recommend