Meet in the Middle Herman Galioulline 4 Sum
- Slides: 24
Meet in the Middle Herman Galioulline
4 Sum - Intro a +b+ c +d= 0
4 Sum - Example Given array A = {1, 0, -2, 2} Target 0 Solution set? (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2) Implementation? How fast can the algorithm be?
4 Sum – Naïve O(n 4)
4 Sum - Smarter Insight! a+b+c+d=0 a + b = -(c + d) O(n 2)
BFS - Overview Start at given node F C A B D H 0 G E
BFS - Overview Start at given node F C Visit all adjacent nodes in arbitrary order A B D H 0 G E 1 Nodes visited: D
BFS - Overview Start at given node F C Visit all adjacent nodes in arbitrary order A B D H 0 G E 1 Nodes visited: D, C
BFS - Overview Start at given node F C Visit all adjacent nodes in arbitrary order A B D H 0 G E 1 Nodes visited: D, C, E
BFS - Overview Start at given node F C Visit all adjacent nodes in arbitrary order A B D H 0 G E 1 Nodes visited: D, C, E, F
BFS - Overview F When all nodes visited, expand frontier C A B D H 0 G E 2 1 Nodes visited: D, C, E, F, G
BFS - Overview F When all nodes visited, expand frontier C A B D H 3 0 G E 2 1 Nodes visited: D, C, E, F, G, H
BFS - Overview 4 F When all nodes visited, expand frontier C A B D H 3 0 G E 2 1 Nodes visited: D, C, E, F, G, H, A
BFS - Overview Done! 4 F C A B D H 3 0 G E 2 1 Nodes visited: D, C, E, F, G, H, A, B
Bi-Directional BFS • • • Large state space Start and goal node Depth Branching factor O(bd) vs. O(bd/2)
Applied to 4 Sum A = {1, 0, -2, 2} 1, 1 0, 0 x, y 1, 3 0, 3 -1, 1 -1, 3 -2, 1 -2, 3 2, 1 2, 3 x: = Current sum y: = # integers summed 0, 4
Bi-BFS & Hash Correspondence
EAPC 06 F - Problem
EAPC 06 F – Problem Visualization
EAPC 06 F - Solution • State space – All possible permutations 15 • Start and end state Input and sorted • Branching factor – Shifts & Intervals n 3 • Depth - Transpositions 4
EAPC 06 F – Solution Visualization Branching Factor: 5 Depth: 4 b c a a y x d w e v f u a: = Permutation b: = Different permutation Related problems: BAPC 14 K NWERC 14 I z
Breaking 2 DES
Solving 8 Puzzle
Questions & Feedback
- Herman intermediate school
- Till we meet till we meet at jesus feet
- Int sum(int a int n) int sum=0 i
- Meet me in the middle rick wormeli
- Des in network security
- Bi 4sum
- Which three continents meet in the middle east
- Herman servotte
- Herman the human cannonball is launched
- Verfassunglehre
- Herman ruslim
- Herman ruslim
- How does the web work?
- Herman schulze
- Teoria multialmacen de la memoria
- Ned herman
- Herman hollerith
- Why did govworks fail
- Herman michell
- Herman vermeer
- Herman muller
- Rezulli
- Herman de hoogh
- Moby dick symbolism
- Herman dikland