Make the best or greedy choice at any given step.
Solves an optimization problem piece-by-piece by making choices that are:
Why — Approximations can be wildly faster than precise ones.
Applies to Problems with:
Optimal Solutions:
Approximations:
Spanning Tree: A spanning tree of G is a connected acyclic subgraph that has all of G’s vertices.
Minimum Spanning Tree: A weighted version of the above.
Any efficient network design!
Grow a MST by repeatedly adding the least-cost edge that connects a vertex in a the existing tree to a vertex not in the existing subtree.
TODO Describe algorithm in more detail.
Grow a MST by repeatedly adding the last-cost edge that doesn’t introduce a cycle among the edge included so far.
Key Question: How do you determine if its acyclic still?
If the two nodes being connected:
- belong to the same connected component, it’ll generate a cycle!
- don’t belong to the same connected component, it’s safe!
Union-Find Problem
??? TODO this is a requirement for Kruskal’s? (to find cycles?)
I have no freaking clue I wanna go home and eat.
Example: ???
Let S = \{ 1,2,3,4,5,6 \}
Applying MakeSet() six times, we initialize the structure of a selection of 6 singletons.
TODO ????
TODO Describe algorithm in more detail.
Okay so we make every node a singleton, sort it, and go smallest edge first and start growing the union efficiently in a way that doesn’t create a cycle.
Sorting is the main thing O(m log m). MakeSet O(n), FindSet O(m), and Union O(n) got nothing on it..
Remember — Do not mismatch this problem with a problem in the last chapter (Floyd’s algortihm). This problem is about one node’s relationship to everyr other node! Floyd’s was about every node!
Problem: Given a weighted connected graph G, find shortest paths for soruce vertex s to each of the other vertices.
Actually pretty similar to Prim’s MST.
Among vertices not already in the tree, finds vertex u with the smallest sum.
d_v + w(v,u)
where:
Aside — A* is an extended version of this.
TODO draw Example graph
Tree Vertices Remaining Vertices a(-,0) b(a,3), c(-,infin), d(a,7), e(-,infin) b(a,3) c(b,3+4), d(b,3+2), e(-,infin) d(b,5) c(b,7), e(d, 5+4) c(b,7) e(d, 9)
TODO Example with airports.
BWI(-,0)
- JFK(BWI, 184)
- MIA(BWI, 946)
- ORD(BWI, 621)
JFK(BWI, 184)
- ORD(JFK, 740 + 184)
- BOS(JFK, 187 + 184)
- PVD(JFK, 144 + 184)
- MIA(JFK, 946 + 184)
- ??? (more remaining, actually)
…
…
ORD(BWI, 621)
- DFW(ORD, 1542)
- MIA(BWI,946)
- SFO(ORD,2462)
MIA(BWI,946)
- LAX(MIA, 2343 + 946 = 3289)
- LAX(DFW, 1235 + 1542 = 2777)
- LAX(SFO, 337 + 2462 = 2799)
Basically we find “what is the shortest path to this node?” and then build the shortest path by gradually extending and building paths out of these optimal little legos.
TODO Draw the diagram
a(-,0)
d(a,7)
b(d,9)
c(d,12)
e(c,18)
Aside: Notes
Coding: Assignment of bit strings to alphabet characters
Codewords: Bit strings assigned for characters of alphebet.
Two Types of Codes:
Problem: If the frequencies of the character occurence are know (e.g., Standard American English), what’s the best binary prefix-free code?
Definition — Prefix-free codes: No codeword is a prefix of another codeword.
TODO Explanation
TODO Basically, it’s an optimal prefix code generated by creating a binary tree using the frequency of the character as like a weight or something. Eerie eyes seen near lake Okay so the encoded file is pretty cool. We get like 25—30% savings.Example: Building tree
E | |
R | |
I | |
Y | |
S | |
N | |
A | |
L | |
K | |
. | |