Graphs 1 Kevin Kauffman CS 309s

advertisement
Graphs 1
Kevin Kauffman
CS 309s
What are Graphs
• Pie graphs? Bar graphs? WRONG
• Graphs simply show things and their
relationship to one another
Examples
• Actors and who they were in movies with
(oracle of bacon)
• Family members and who is related to whom
(family tree)
• Intersections and the roads that connect them
• Board game spaces and which moves you can
make
Terminology
• Node (vertex): the individual thing we are
keepting track of (intersection, family
member, actor, game space)
• Edge: the relationship between TWO nodes
(road, relationship, game move, shared movie)
• A given set of nodes and edges is called a
graph
Graph Dichotomies
• Directed/Undirected
– Edges may be one way (“go directly to jail” in monopoly) or
both ways (two actors in the same movie)
• Weighted/Unweighted
– Edges may have a value which represents some quality
(length of a road between two intersections) or every edge
can be valued equally (actors in the same movie)
• Cyclic/Acyclic
– Can you start at one node, and follow edges (any one edge
at most once) and get back to the start node (a cycle)?
– Undirected, Acyclic graphs are known as TREES
Graph Representations
Adjacency Matrix
• 2-D array indicating whether there is an edge
between two given nodes
• int[][]
• In unweighted graphs, int[i][j]=1 indicates
edge, while 0 indicates no edge
• In weighted graphs, int[i][j] indicates length
– 0,-1, or some huge number may indicate no edge
• O(n^2) storage, O(n^2) iterate through all
edges….WASTEFUL
Adjacency List
• Only store edges which actually exist (instead of
all possible edge)
• For each node, store each node to which it is
connected
• Unweighted: ArrayList<ArrayList<Integer>>
• Weighted: Arraylist<HashMap<Integer, Integer>>
• O(E) to iterate through all edges (better) but pain
in the neck to initialize and use
What can we do with graphs?
• Loads of stuff! (and we’ll get to lots of it
throughout the semester)
• Calculate distance between nodes
• Figure out which nodes can be reached from
others
• Figure out which edges we can remove and
still get to all the other nodes
• ?????
Distance in an Unweighted Graph
• If we have an unweighted graph (can be either
directed or not), how can we figure out the
minimum number of edges we must traverse
to get from some node s to another node t?
• Ideas?
Brute Force Every Path
• Start at s and follow every edge there
• At each node you reach, follow every edge
there, recording the distance to each node
• At the end, take the minimum distance to the
node
• Problem? Takes O(n!) time to compute!
Key Intuition
• We need to come up with a way to only visit a
given node once
• If we can visit all the nodes 1 away from s first,
then we don’t need to visit them again
(because we know we can’t beat that!)
• If we can visit all the nodes 2 away from s
next, we don’t need to visit THEM again (since
we already know they can’t be 1 away)
Distance (overview)
1. Can we find all nodes 1 away from s?
– Yes…all nodes which have an edge going to s must
be at most 1 away from s
2. Can we find all nodes 2 away from s?
– Yes… all the nodes (which we haven’t visited)
which have an edge from one of the step 1 nodes
must be 2 away from s
3. By induction, we can find the distance to all
the nodes
Breadth First Search (BFS)
q.add(s)
while(q isn’t empty)
on=q.poll()
for(edge in on) if (other node unvisited)
distace(other node)=distance(on)+1
q.add(other node)
What’s going on?
• We visit all the nodes in order of how far they
are away
• Since we directly reference the distance to the
node as we pop it off the queue, we don’t
have to worry about tracking exactly how far
we are away
• Instead of updating distance, we can do other
things (perhaps add a “previous” value to get
the “turn by turn” of what the path actually is)
Depth First Search (DFS)
• Similar to BFS, but visit all nodes in a given
sub-graph before entering another sub-graph
(rather than visiting all nodes 1 away first)
• Use a stack instead of a queue
Example
• GNYR 2009 I, Theta Puzzle
Download