150 likes | 419 Views
Divide & Conquer BFS , DFS CSCI3160 tutorial (3 rd week) Office: SHB 913 Office Hour: ( Mon ) 10:00 -12:00 Email: jjye @cse.cuhk.edu.hk. Ye Junjie. Outline. Hidden Line Removal (Divide & Conquer) Data structures Queue for breadth-first search (BFS) Stack for depth-first search (DFS).
E N D
Divide & ConquerBFS, DFSCSCI3160 tutorial (3rd week)Office: SHB 913 Office Hour: (Mon) 10:00 -12:00 Email: jjye@cse.cuhk.edu.hk Ye Junjie
Outline Hidden Line Removal (Divide & Conquer) Data structures Queue for breadth-first search (BFS) Stack for depth-first search (DFS)
hidden line removal Design a O(nlogn) algorithm to output all visible lines from n non-vertical lines. A visible skyline
representation How to represent a line: How to represent a skyline: y=ax+b 2 parameters a, b A skyline Turning points x1,x2,x3,x4 + line segment S1,…,S5 S1 S5 x1 x3 S2 x4 S4 Skyline Profile record x2 S3 Group them into pairs
divide and conquer O(n log n)means Divide and Conquer:T(n) = 2T(n/2) + O(n) Divide n lines into 2 groups (blue and black) skyline2 combined skyline skyline1
intersection How to combine 2 lines: (a) No intersection [i.e. parallel] (b) Intersection L1: y= mx+b1 L2: y= mx+b2 b1>b2 L1 dominant L2 L2: y= a2 x+b2 L1: y= a1 x+b1 Find (x’,y’) L1: y= 3x-2 L2: y= -x+10 L1- L2 x’=3 , y’=7 for x<3, y2> y1 for x>3, y2< y1 (x’, y’) e.g. (3,7) Skyline profile:
intersection Core problem: How to combine 2 skylines? Similar to the procedure of combining 2 lines.
intersection Combine 2 skylines Deal first line segment of skyline1 and skyline2. Case 1: Intersection at (x’, y’) If line1 is above line 2 at segment (-inf, x’) :
intersection Combine 2 skylines Deal first line segment of skyline1 and skyline2. Case 2: No intersection & skyline1 above skyline2 & skyline1 below skyline2 & skyline1 above skyline2 & skyline1 below skyline2 & skyline1 above skyline2 & skyline1 below skyline2 Change skyline1 and skyline2, and add new segment to skyline3.
Data structures Adjacency list The representation of all edge in an undirected graph as a list We keep, for each vertex in the graph, a list of all other vertices which it has an edge to. Stack last-in, first-out PUSH: insert new item to stack POP: delete top item from stack Queue first-in, first-out push(), pop(), empty(), top() 1 2 3 4 5 6 7 1 2 3 4 2 1 3 5 6 3 1 2 6 4 1 6 5 2 6 2 3 4 7 7 6
Exploring graphs Input: a graph G, and a source vertex s Output: find all nodes reachable from the source BFS: choose frontier edge incident to least recently visited vertex Using a queue DFS: choose frontier edge incident to most recently visited vertex Using a stack
Example of breadth-first search (BFS) Procedure BFS (G, s): create a queue Q PUSH source onto Q mark s while Q is not empty: get top item v from Q find an frontier edge e incident on vinGraph:{ let w be the other end of e if w is not marked: mark w PUSH w onto Q } if there is no such edge e exists: POP Q 1 2 3 4 5 6 7 Q 1 2 3 4 5 6 7
Example of depth-first Search (DFS) Procedure DFS (G, s): create a stack S PUSH source onto S mark s while S is not empty: get top item v from S find an frontier edge e incident on v in Graph{ let w be the other end of e if w is not marked: mark w PUSH w onto S } if there is no such edge e exists: POP S 1 2 3 4 7 4 6 5 6 7 5 3 2 1 S
Thank you! Q&A