Greedy Algorithms

advertisement
Graphs 2
Kevin Kauffman
CS 309s
Problem
• We have sprinklers in our yard which need to
be connected with pipe. Assuming pipes may
only intersect at sprinkler heads, what is the
minimum amount of pipe we need to connect
the sprinklers?
Minimum Spanning Tree
• Set of edges which
– Connect all the nodes
– Has the minimum total weight of all edges
• Contains no cycles
• If there are multiple MSTs for a given graph,
they all contain the same individual edge
weights
• If on an xy plane, it will not self intersect
Calculating the MST
• Create a set of connected nodes
• Add a random node to “seed” the set
• While the set doesn’t contain all nodes
– Find the shortest edge which goes from a node in
the set to a node out of the set
– Add the edge to the MST and the other node to
the set
Implementation
• Naïve: iterate through all edges in each cycle,
and take the minimum which satisfies the
in/out requirement
– O(V*E)
• Better: maintain sorted list of edges which
touch the nodes in the set, add to it as we go
– O(E*log(E))
Maintaining Sorted List
• Use int[2] or java.awt.point to represent edges
• Place them on a TreeSet when we see them,
which will sort them in the order specified by
our:
Custom Comparator
Custom Comparator
• Takes input A and B; returns an int
– Returns positive if A comes before B
– Returns 0 if A is the same as B
– Returns negative if A comes after B
• Rules:
– Commutative: if comp(A,B)<0, then comp(B,A)>0
– MUST break ties (or comparator will think A and B
are the same and delete one of them)
• This must still adhere to rule 1
Shortest Distance
• BFS allows us to calculate distance in
unweighted graphs, how do we do it in
weighted graphs?
• For an edge of length n, insert n-1 “fake”
nodes, so all edges are length 1
• Graph is now unweighted
• Run BFS!
A Better Way
• If we add nodes proportional to edge length,
runtime now depends on edge length (bad for
long edges)
• Instead of iterating over all the fake nodes can
we calculate the “real” node which we are
going to reach next?
Dijkstra’s Algorithm
• Lemma: if we are at a node, we can calculate
the next node BFS would visit by comparing
edge lengths
• Start at start node
• Sort edges from start node to find next node
visited (second node)
– How do we find the the third node, since it could
be connected to the second node, but not the
start node?
Dijkstra’s Algorithm
• Because of Lemma, we can calculate the distance
from the second node to all the nodes its
connected to
• Total distance to those nodes is distance(2nd
node) + weight(edge)
– EXCEPT if we had a shorter distance directly from the
start node
• Sort all those distances and find the actual third
node visited
• REPEAT
Dijkstra’s TL-DR
• Tree set of nodes by distance
• Pop shortest remaining distance (u), make it
permanent
• for all edges from u to an unvisited node (v), if
dist(u)+dist(uv)<dist(v), update dist(v)
• O((E+V)log(E)), doesn’t depend on edge length
Implementation
• Array storing distances (must be final since we
reference it in the compartor)
• Treeset sorting unvisited nodes by distance
• Every time we pop a node and check all its edges,
if we find that ends up being less than current
distance:
– Remove that node from the treeset
– Update its value
– Add it to the treeset
TREESET DOESN’T LIKE VALUES CHANGED UNDER IT
Download