Meet in the Middle Herman Galioulline 4 Sum

  • Slides: 24
Download presentation
Meet in the Middle Herman Galioulline

Meet in the Middle Herman Galioulline

4 Sum - Intro a +b+ c +d= 0

4 Sum - Intro a +b+ c +d= 0

4 Sum - Example Given array A = {1, 0, -2, 2} Target 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 – Naïve O(n 4)

4 Sum - Smarter Insight! a+b+c+d=0 a + b = -(c + d) O(n

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

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

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

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

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

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

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

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

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

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

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

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

Bi-BFS & Hash Correspondence

EAPC 06 F - Problem

EAPC 06 F - Problem

EAPC 06 F – Problem Visualization

EAPC 06 F – Problem Visualization

EAPC 06 F - Solution • State space – All possible permutations 15 •

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

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

Breaking 2 DES

Solving 8 Puzzle

Solving 8 Puzzle

Questions & Feedback

Questions & Feedback