02110 String indexing Computational geometry Introduction to - - PowerPoint PPT Presentation

02110
SMART_READER_LITE
LIVE PREVIEW

02110 String indexing Computational geometry Introduction to - - PowerPoint PPT Presentation

Overview Balanced binary search trees: Red-black trees and 2-3-4 trees Amortized analysis Dynamic programming Network flows String matching 02110 String indexing Computational geometry Introduction to NP-completeness


slide-1
SLIDE 1

02110

Inge Li Gørtz

  • Balanced binary search trees: Red-black trees and 2-3-4 trees
  • Amortized analysis
  • Dynamic programming
  • Network flows
  • String matching
  • String indexing
  • Computational geometry
  • Introduction to NP-completeness
  • Randomized algorithms

Overview

  • 2-3-4 trees.
  • Allow 1, 2, or 3 keys per node
  • Perfect balance. Every path from root to

leaf has same length.

  • Red-black trees.
  • The root is always black
  • All root-to-leaf paths have the same

number of black nodes.

  • Red nodes do not have red children
  • All leaves (NIL) are black

Balanced binary search trees

3

R H N C A

E I A S

A A C H I N E R S

smaller than E between E and R larger than R

  • Self-adjusting BST (Sleator-Tarjan 1983).
  • Most frequently accessed nodes are close to the root.
  • Tree reorganizes itself after each operation.
  • After access to a node it is moved to the root by splay operation.
  • Worst case time for insertion, deletion and search is O(n).
  • Amortized time per operation O(log n).

Splay trees

slide-2
SLIDE 2
  • Splay(x): do following rotations until x is the root. Let y be the parent of x.
  • right (or left): if x has no grandparent.

Splaying

right rotation at x (and left rotation at y)

x y

a b c a b c

y x

right left

  • Splay(x): do following rotations until x is the root. Let p(x) be the parent of x.
  • right (or left): if x has no grandparent.
  • zig-zag (or zag-zig): if one of x,p(x) is a left child and the other is a right

child.

Splaying

zig-zag at x

x x x w w w z z z

a b c d a a b b c c d d

  • Splay(x): do following rotations until x is the root. Let y be the parent of x.
  • right (or left): if x has no grandparent.
  • zig-zag (or zag-zig): if one of x,y is a left child and the other is a right child.
  • roller-coaster: if x and p(x) are either both left children or both right

children.

Splaying

right roller-coaster at x (and left roller-coaster at z)

x x x y y y z z z

a a a b b b c c c d d d

Dynamic set implementations

Worst case running times (except splay trees)

8

Implementation search insert delete minimum maximum successor predecessor linked lists O(n) O(1) O(1) O(n) O(n) O(n) O(n)

  • rdered array

O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) BST O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) red-black tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) splay tree O(log n)† O(log n)† O(log n)† O(log n)† O(log n)† O(log n)† O(log n)† †: amortized running time

slide-3
SLIDE 3

Amortized analysis

  • Amortized analysis.
  • Time required to perform a sequence of data operations is averaged over all the operations

performed.

  • Example: dynamic tables with doubling and halving
  • If the table is full copy the elements to a new array of double size.
  • If the table is a quarter full copy the elements to a new array of half the size.
  • Worst case time for insertion or deletion: O(n)
  • Amortized time for insertion and deletion: O(1)
  • Any sequence of n insertions and deletions takes time O(n).
  • Methods.
  • Aggregate method
  • Accounting method
  • Potential method
  • General algorithmic technique
  • Can be used when the problem have “optimal substructure”:

solution can be constructed from optimal solutions to subproblems.

  • Examples
  • Rod cutting
  • Longest common subsequence
  • Sequence alignment
  • All pairs shortest path

Dynamic Programming

  • subproblem property:

Longest common subsequence

xi Xi-1 yj Yj-1

Depends on B A N A N A S S A N D A L S

LCS(X5, Y4) LCS(Xi, Yj) =      if i = 0 or j = 0 LCS(Xi−1, Yj−1) + 1 if xi = yj max(LCS(Xi, Yj−1), LCS(Xi−1, Yj)) if xi 6= yj

Value, not solution

  • subproblem property:

Longest common subsequence

xi Xi-1 yj Yj-1

B A N A N A S S 1 A 1 1 1 1 1 1 N 1 2 2 2 2 2 D 1 2 2 2 2 2 A 1 2 3 3 3 3 L 1 2 3 3 3 3 S 1 2 3 3 3 4 B A N A N A S S

  • A
  • N
  • D
  • A
  • L
  • S
  • LCS(Xi, Yj) =

     if i = 0 or j = 0 LCS(Xi−1, Yj−1) + 1 if xi = yj max(LCS(Xi, Yj−1), LCS(Xi−1, Yj)) if xi 6= yj

slide-4
SLIDE 4
  • subproblem property:

Longest common subsequence

xi Xi-1 yj Yj-1

B A N A N A S S 1 A 1 1 1 1 1 1 N 1 2 2 2 2 2 D 1 2 2 2 2 2 A 1 2 3 3 3 3 L 1 2 3 3 3 3 S 1 2 3 3 3 4 B A N A N A S S

  • A
  • N
  • D
  • A
  • L
  • S
  • LCS(Xi, Yj) =

     if i = 0 or j = 0 LCS(Xi−1, Yj−1) + 1 if xi = yj max(LCS(Xi, Yj−1), LCS(Xi−1, Yj)) if xi 6= yj

Network Flow

  • Network flow:
  • graph G=(V,E).
  • Special vertices s (source) and t (sink).
  • Every edge (u,v) has a capacity c(u,v) ≥ 0.
  • Flow:
  • capacity constraint: every edge e has a flow 0 ≤ f(u,v) ≤ c(u,v).
  • flow conservation: for all u ≠ s, t: flow into u equals flow out of u.
  • Value of flow f is the sum of flows out of s minus sum of flows into s:
  • Maximum flow problem: find s-t flow of maximum value

1 2 2 2 2 1 2 2 1 s t

X

v:(v,u)∈E

f(v, u) = X

v:(u,v)∈E

f(u, v)

u

|f| = X

v:(s,v)∈E

f(s, v) − X

v:(v,s)∈E

f(v, s)

Network flow: s-t Cuts

  • Cut: Partition of vertices into S and T, such that s ∈ S and t ∈ T.
  • Capacity of cut: total capacity of edges going from S to T.
  • Flow across cut: flow from S to T minus flow from T to S.
  • Value of flow any flow |f| ≤ c(S,T) for any s-t cut (S,T).
  • Suppose we have found flow f and cut (S,T) such that |f| = c(S,T). Then f is a

maximum flow and (S,T) is a minimum cut.

s t S T

  • Augmenting path (definition different than in CLRS): s-t path where
  • forward edges have leftover capacity
  • backwards edges have positive flow
  • There is no augmenting path <=> f is a maximum flow.
  • Ford-Fulkerson algorithm:
  • Repeatedly find augmenting path, use it, until no augmenting path exists
  • Running time: O(|f*| m).
  • Edmonds-Karp algorithm:
  • Repeatedly find shortest augmenting path, use it, until no augmenting path exists
  • Use BFS to find a shortest augmenting path.
  • Running time: O(nm2)
  • Find minimum cut. All vertices to which there is an augmenting path from s goes into

S, rest into T.

Augmenting paths

s t

f1 < c1 f2 > 0 f3 < c3 f4 < c4 f5 > 0 f6 > 0 +δ +δ +δ

  • δ
  • δ
  • δ
slide-5
SLIDE 5
  • Can model and solve many problems via maximum flow.
  • Maximum bipartite matching
  • k edge-disjoint paths
  • capacities on vertices
  • Many sources/sinks
  • assignment problems: Example. X doctors, Y holidays, each doctor should work

at at most c holidays, each doctor is available at some of the holidays.

Network flow

s t

1 1 c c c

  • String matching problem:
  • string T (text) and string P (pattern) over an alphabet Σ. |T| = n, |P| = m.
  • Report all starting positions of occurrences of P in T.
  • String matching automaton. Running time: O(n + m|Σ|)
  • Knuth-Morris-Pratt (KMP). Running time: O(m + n)
  • Rabin-Karp (fingerprinting). Running time: Expected O(m + n)

String Matching

Finite Automaton

  • Finite automaton: alphabet Σ = {a,b,c}. P= ababaca.

a b a b a c a a a a b a b

starting state accepting state

Finite Automaton

  • Finite automaton: alphabet Σ = {a,b,c}. P= ababaca.

a b a b a c a a a a b a b

starting state accepting state longest prefix of P that is a suffix of ‘abaa'

slide-6
SLIDE 6

Knuth-Morris-Pratt (KMP)

  • Matched P[1…q]: Find longest block P[1..k] that matches end of P[2..q].
  • Find longest prefix P[1...k] of P that is a proper suffix of P[1...q]
  • Array π[1…m]:
  • π[q] = max k < q such that P[1...k] is a suffix of P[1…q].
  • Can be seen as finite automaton with failure links:

a a a b a a a a b a b a a a a b a b a

i 1 2 3 4 5 6 7 π[i] 1 2 3 1

a b a b a c a

1 2 3 4 5 6

String Indexing

  • String indexing problem. Given a string S of characters from an alphabet Σ.

Preprocess S into a data structure to support

  • Search(P): Return starting position of all occurrences of P in S.
  • Tries.
  • Compressed trie. Chains of nodes with a single child merged into a single

node

  • Suffix tree. Compressed trie over all the suffixes of a string.
  • Suffix tree. Compressed trie over all suffixes of a

string.

  • Suffix trees can be used to solve the String indexing

problem in:

  • Space: O(n)
  • Search time: O(m+occ)
  • Preprocessing: O(sort(n,|Σ|)) time

Suffix tree

a $ $ $

1 2 3 4 5

$

6

banana

na

na

na

na

  • Suffix tree. Compressed trie over all suffixes of a

string.

  • Suffix trees can be used to solve the String indexing

problem in:

  • Space: O(n)
  • Search time: O(m+occ)
  • Preprocessing: O(sort(n,|Σ|)) time

Suffix tree

0 1 2 3 4 5 6 b a n a n a $

1 2 3 4 5 6

[2,3] [4,6]

[1] [6] [6]

[0,6] [2,3] [4,6]

[6] [6]

slide-7
SLIDE 7
  • Find longest common suffix of strings S1 and S2.
  • Construct the suffix tree over S1$1S2$2 (remove all parts of suffixes crossing $1).
  • Example: Find longest common substring of ananas and banana:
  • Construct suffix tree of ananas$1banana$2.
  • Mark bottom up all nodes which has both $1 and $2 in a descendant.
  • The “deepest” one (the one representing the longest string) is the longest common

substring.

Longest common substring

na a $2 $2 na na $2 s$1 s$1 na $2 s$1 s$1 s$1 s$1 $2 $2 $1

anana is the longest common substring

banana$2

  • Geometric problems (this course Euclidean plane).
  • Convex hull
  • Jarvis’s march O(nh)
  • Graham’s scan O(n log n)
  • Counterclockwise test in O(1) time

Computational Geometry

  • 3 equivalent definitions of convex hull: Given set of points Q, the convex hull CH(Q)

is

  • Def 1. The smallest convex polygon containing Q.
  • Def 2. The largest convex polygon, whose vertices all are points in Q.
  • Def 3. The convex polygon containing Q and whose vertices all are points in Q.
  • Last time
  • Graham’s scan O(n log n)
  • Jarvis’s march O(nh)

Convex Hull

p1 p2 p3 p4 p5 p6

  • Graham’s scan.
  • Pick lowest point p0 as starting point
  • Sort remaining points in counterclockwise order around p0.
  • Use linear time scan to build hull:
  • Push p0, p1 and p2 onto the stack.
  • Next point p:
  • If adding p gives a left turn push p onto stack
  • If adding p gives a right turn pop top element from stack and check again.

Continue checking until we get a right turn or only 3 vertices left on stack.

Graham’s scan

p0 p1 p2 p3 p4 p5 p6 p7 p8 p9

slide-8
SLIDE 8
  • Start with adding lowest point p0 to CH(Q).
  • Next point after p:
  • point appearing to be furthest to the right to someone standing at p and looking

at the other points (smallest if sorted in counterclockwise order).

  • If q point following p then for any other point r in Q then p,q,r are in

counterclockwise order.

  • Can find next vertex by performing n-1 counterclockwise tests.

Jarvis’s march

  • Median/Select.
  • Quick-sort

Randomized algorithms

  • P solvable in deterministic polynomial time.
  • NP solvable in non-deterministic (with guessing) polynomial time. Only the time for

the right guess is counted.

  • P⊆NP (every problem T which is in P is also in NP).
  • It is not known (but strongly believed) whether the inclusion is proper, that is whether

there is a problem in NP which is not in P .

  • There is subclass of NP which contains the hardest problems, NP-complete

problems.

  • Reductions.

P and NP

Thank you