|
|
|
|
|
by catears
1263 days ago
|
|
I have one example and an anti-example. Both are related to algorithms and computer science. A) Packing a binary tree into an array. Anyone that has attended an algorithms course has likely created a binary tree with nodes, leaf nodes, left and right child etc. Seen the pine-tree like sketch with a larger example where each node except the leaf nodes have a left and right child. So how do you pack this tree into an array and traverse it efficiently? Well you turn it 90 degrees side-ways, slightly shift all nodes on the same level so that none align and put them into an array by going from leftmost to rightmost. (or other way depending on if you shifted 90 degrees or -90 degrees). Congrats, you've packed nodes into an array. How do you traverse it? Our root node is at index 1 and if you packed the array correctly then `idx = (idx * 2) + 1` will move down one side and `idx = (idx * 2) + 0` moves down the other. I don't have a good visual explanation of this but you can think of the integer/index as a bit-sequence describing when in the tree a left vs. right path was taken (with the exception of the root node). B) Anti-example: Ford Fulkerson algorithm for finding shortest paths between all nodes in a graph. The algorithm is basically just three for-loops stacked on top of each other, but I still can't grasp why it works. Something with dynamic programming and incrementally building on prior established intermediate paths. The algorithm is truly the product of a beautiful mind. |
|
B) Did you mean Floyd-Warshall's?