bfs and dfs bfs and dfs in directed graphs bfs in undirected graphs an improved undirected...

Download BFS and DFS BFS and DFS in directed graphs BFS in undirected graphs An improved undirected BFS-algorithm

Post on 18-Dec-2015




3 download

Embed Size (px)


  • Slide 1
  • BFS and DFS BFS and DFS in directed graphs BFS in undirected graphs An improved undirected BFS-algorithm
  • Slide 2
  • The Buffered Repository Tree (BRT) Stores key-value pairs (k,v) Supported operations: I NSERT (k,v) inserts a new pair (k,v) into T E XTRACT (k) extracts all pairs with key k Complexity: I NSERT : O((1/B)log 2 (N/B)) amortized E XTRACT : O(log 2 (N/B) + K/B) amortized (K = number of reported elements)
  • Slide 3
  • The Buffered Repository Tree (BRT) (2,4)-tree Leaves store between B/4 and B elements Internal nodes have buffers of size B Root in main memory, rest on disk Main memory Disk
  • Slide 4
  • Main memory Disk I NSERT (k,v) O(X/B) I/Os to empty buffer of size X B Amortized charge per element and level: O(1/B) Height of tree: O(log 2 (N/B)) Insertion cost: O((1/B)log 2 (N/B)) amortized Main memory Disk
  • Slide 5
  • Elements with key k E XTRACT (k) Number of traversed nodes: O(log 2 (N/B) + K/B) I/Os per node: O(1) Cost of operation: O(log 2 (N/B) + K/B) But careful with removal of extracted elements Main memory Disk Main memory Disk
  • Slide 6
  • Cost of Rebalancing O(N/B) leaf creations and deletions O(N/B) node splits, fusions, merges Each such operation costs O(1) I/Os O(N/B) I/Os for rebalancing Theorem: The BRT supports I NSERT and E XTRACT operations in O((1/B)log 2 (N/B)) and O(log 2 (N/B) + K/B) I/Os amortized.
  • Slide 7
  • Directed DFS Algorithm proceeds as internal memory algorithm: Use stack to determine order in which vertices are visited For current vertex v: Find unvisited out-neighbor w Push w on the stack Continue search at w If no unvisited out-neighbor exists Remove v from stack Continue search at vs parent Stack operations cost O(N/B) I/Os Problem: Finding an unvisited vertex
  • Slide 8
  • Directed DFS Data structures: BRT T Stores directed edges (v,w) with key v Priority queues P(v), one per vertex Stores unexplored out-edges of v Invariant: Not in P(v) In P(v) and in T In P(v), but not in T
  • Slide 9
  • Directed DFS Finding next vertex after vertex v: v E XTRACT (v): Retrieve red edges from T Remove these edges from P(v) using D ELETE Retrieve next edge using D ELETE M IN on P(v) Insert in-edges of w into T w Push w on the stack O(log 2 (|E|/B) + K 1 /B) O(sort(K 1 )) O(1 + (K 2 /B)log 2 (|E|/B)) O(1/B) amortized O((1/B)log m (|E|/B)) O(|V| log 2 (|E|/B) + |E|/B) O(|V| + sort(|E|)) O((|E|/B)log 2 (|E|/B)) O(|V|/B) O(sort(|E|)) Total: O((|V| + |E|/B)log 2 (|E|/B))
  • Slide 10
  • Directed DFS + BFS BFS can be solved using same algorithm Only modification: Use queue (FIFO) instead of stack Theorem: Depth first-search and breadth-first search in a directed graph G = (V,E) can be solved in O((|V|+|E|/B)log 2 (|E|/B)) I/Os. Exercise: Convince yourself that the priority queues P(v) are not necessary in the case of BFS.
  • Slide 11
  • Undirected BFS Observation: For v L(i), all its neighbors are in L(i 1) L(i) L(i + 1). Build BFS-tree level by level: Initially, L(0) = {r} Given levels L(i 1) and L(i): Let X(i) = set of all neighbors of vertices in L(i) Let L(i + 1) = X(i) \ (L(i 1) L(i)) Partition graph into levels L(0), L(1),... around source: L(0), L(1), L(2), L(3)
  • Slide 12
  • Undirected BFS Constructing L(i + 1): Retrieve adjacency lists of vertices in L(i) X(i) Sort X(i) Scan L(i 1), L(i), and X(i) to Remove duplicates from X(i) Compute X(i) \ (L(i 1) L(i)) Complexity: O(|L(i)| + sort(|L(i 1)| + |X(i)|)) I/Os O( ) I/Os|V| +sort(|E|) Theorem: Breadth-first search in an undirected graph G = (V,E) can be solved in O(|V| + sort(|E|)) I/Os.
  • Slide 13
  • A Faster BFS-Algorithm Problem with simple BFS-algorithm: Random accesses to retrieve adjacency lists Idea for a faster algorithm: Load more than one adjacency list at a time Reduces number of random accesses Causes edges to be involved in more than one iteration of the algorithm Trade-off
  • Slide 14
  • A Faster BFS-Algorithm (Randomized) Let 0 < < 1 be a parameter (specified later) Two phases: Build |V| disjoint clusters of diameter O(1/ ) Perform modified version of S IMPLE B FS Clusters C 1,...,C q formed using BFS from randomly chosen set V = {r 1,...,r q } of masters Vertex is chosen as a master with probability (coin flip) Observation: E[|V|] = |V|. That is, the expected number of clusters is |V|.
  • Slide 15
  • Forming Clusters (Randomized) Apply S IMPLE B FS to form clusters L(0) = V v C i if v is descendant of r i s
  • Slide 16
  • Forming Clusters (Randomized) Lemma: The expected diameter of a cluster is 2/ . E[k] 1/ Corollary: The clusters are formed in expected O((1/ )sort(|E|)) I/Os. x v1v1 v2v2 v3v3 v4v4 v5v5 s vkvk
  • Slide 17
  • Forming Clusters (Randomized) Form files F 1,...,F q, one per cluster F i = concatenation of adjacency lists of vertices in C i Augment every edge (v,w) F i with the start position of file F j s.t. w C j : Edge = triple (v,w,p j ) s
  • Slide 18
  • The BFS-Phase Maintain a sorted pool H of edges s.t. adjacency lists of vertices in L(i) are contained in H Scan L(i) and H to find vertices in L(i) whose adjacency lists are not in H Form list of start positions of files containing these adjacency lists and remove duplicates Retrieve files, sort them, and merge resulting list H with H Scan L(i) and H to build X(i) Construct L(i + 1) from L(i 1), L(i), and X(i) as before O((|L(i)| + |H|)/B) O(sort(|L(i)|)) O(K + sort(|H|) + |H|/B) O((|L(i)| + |H|)/B) O(sort(|L(i)| + |L(i 1)| + |X(i)|))
  • Slide 19
  • The BFS-Phase I/O-complexity of single step: O(K + |H|/B + sort(|H| + |L(i 1)| + |L(i)| + |X(i)|)) Expected I/O-complexity: O( |V| + |E|/( B) + sort(|E|)) Choose Theorem: BFS in an undirected graph G = (V,E) can be solved in I/Os.
  • Slide 20
  • Single Source Shortest Paths The tournament tree SSSP in undirected graphs SSSP in planar graphs
  • Slide 21
  • Single Source Shortest Paths Need: I/O-efficient priority queue I/O-efficient method to update only unvisited vertices
  • Slide 22
  • The Tournament Tree =I/O-efficient priority queue Supports: I NSERT (x,p) D ELETE (x) D ELETE M IN D ECREASE K EY (x,p) All operations take O((1/B)log 2 (N/B)) I/Os amortized Note: N = size of the universe # elements in the tree
  • Slide 23
  • The Tournament Tree Static binary tree over all elements in the universe Elements map to leaves, M elements per leaf Internal nodes have signal buffers of size M Root in main memory, rest on disk Main memory Disk Internal nodes store between M/2 and M elements
  • Slide 24
  • Main memory Disk The Tournament Tree Elements stored at each node are sorted by priority Elements at node v have smaller priority than elements at vs descendants Convention: x T if and only if p(x) is finite
  • Slide 25
  • The Tournament Tree Deletions Operation D ELETE (x) signal D ELETE (x) x D ELETE (x) U PDATE (x, ) v
  • Slide 26
  • The Tournament Tree Insertions and Updates Operations I NSERT (x,p) and D ECREASE K EY (x,p) signal U PDATE (x,p) x w v Current priority p If p < p: Update If p p: Do nothing All elements < p Forward signal to w At least one element p Insert x Send D ELETE (x) to w
  • Slide 27
  • The Tournament Tree Handling Overflow Let y be element with highest priority p y Send signal P USH (y,p y ) to appropriate child of v y w v
  • Slide 28
  • The Tournament Tree Keeping the Nodes Filled w v O(M/B) I/Os to move M/2 elements one level up the tree
  • Slide 29
  • Main memory Disk The Tournament Tree Signal Propagation Scan vs signal, partition into sets X u and X w Load u into memory, apply signals in X u to u, insert signals into us signal buffer Do the same for w O((|X| + M)/B) = O(|X|/B) I/Os
  • Slide 30
  • The Tournament Tree Analysis Elements travel up the tree Cost: O(1/B) I/Os amortized per element and level O((K/B)log 2 (N/B)) I/Os for K operations Signals travel down the tree Cost: O(1/B) I/Os amortized per signal and level O(K) signals for K operations O((K/B)log 2 (N/B)) I/Os Theorem: The tournament tree supports I NSERT, D ELETE, D ELETE M IN, and D ECREASE K EY operations in O((1/B)log 2 (N/B)) I/Os amortized.
  • Slide 31
  • Single Source Shortest Paths Modified Dijkstra: Retrieve next vertex v from priority queue Q using D ELETE M IN Retrieve vs adjacency list Update distances of all of vs neighbors, except predecessor u on the path from s to v Repeat O(|V| + (E/B)log 2 (V/B)) I/Os using tournament tree
  • Slide 32
  • Single Source Shortest Paths Problem: Observation: If v performs a spurious update of u, u has tried to update v before. Record this update attempt of u on v by insterting u into another priority queue Q Priority: d(s,u) + w({u,v}) u v
  • Slide 33
  • Single Source Shortest Paths Second modification: Retrieve next vertex using two D ELETE M IN s, one on Q, one on Q Let (x,p x ) be the element retrieved from Q, let (y,p y ) be the element retrieved from Q If p x p y : re-insert (y,p y ) into Q and proceed as normal If p x < p y : re-insert (x,p x ) into Q and perform a D ELETE (y) on Q
  • Slide 34
  • Single Source Shortest


View more >