Overview
What will the final exam be like?
Paper
Exact Java not necessary.
Probably including the formal definition of Big-O, or applying that.
No unit tests.
Will the mentors hold a review session in which they discuss the gibberish that is the definition of Big-O?
Yes.
Have you decided on a format?
I like “four right is an A, three right is a B, two right is a C, one right is a D”.
Will we receive the quizzes back so that we can study from them?
I hope so.
Will we get a grade report before the final?
Almost certainly.
Computer scientists are practical folk. We like to take real problems and find ways to solve them using algorithms (performed by the computer).
Doing so often requires that we “model” the problems, trying to extract the key parts, throwing away unnecessary details.
We’re going to look at a set of geographical problems and think about how we might represent them.
Some sample problems:
In the abstract, all of these problems involve a set of “things” some of which are connected. Each connection has an associated “cost” (also “weight”).
We then have questions (which is closest, which subset do I need to clear, etc)?
A graph is a collection of vertices and edges. Edges connect vertices.
Other important graph terminology
Question: How do we represent vertices?
Observers - Extract information
Iterator<Edge> edgesFrom(int vertex)
int getWeight(int vFrom, int vTo)
vFrom
to vTo
?hasEdge
likely adds expense.Edge getEdge(int vFrom, int vTo)
hasVertex(int vertex)
- determine if the vertex is in the graphint totalWeight()
- find the total weight (may need a use case)List<Integer> neighbors(int vertex)
- Get a list of the neighboring nodes.
edgesFrom
.Iterator<Integer> vertices()
Iterator<Edge> edges()
numVertices()
numEdges()
Mutators - Change the graph
addVertex(int vertex)
- Add a new vertex
int addVertex()
- Returns the number of the new vertexaddEdge(int vFrom, int vTo, int weight)
removeVertex(int vertex)
- GuessremoveEdge(int vFrom, int vTo)
- SimilarchangeWeight(int vFrom, int vTo, int newWeight)
Additional - Things we might build from the basic operations
isConnected()
- is there a path from every vertex to every other
vertexshortestPath(from,to)
- what is the length of the shortest path
between from and to (or what are the nodes on that path)ArrayList<ArrayList<Integer>> routesBetween(from, to)
How might you represent a Graph in Java? How much will the basic operations cost?
Sample Graph
v1 -----> v2 -----> v3
| ^
v |
v4---------/
An Edge is a triplet: From, To, Weight
Version 1: List of all the edges (and a list of vertices)
Costs
Edge getEdge(int vFrom, int vTo)
- O(m)List<Edge> edges()
- O(1) to get the list, O(m) to visit eachList<Integer> vertices()
- O(1) to get the list, O(n) to visit eachList<Edge> edgesFrom(int vertex)
- O(m) to get, O(n) to visitVersion 2: An array of lists of edges. A[i] = edges from vertex i.
Edge getEdge(int vFrom, int vTo)
- Get the list at A[vFrom], iterate
that list until you find one whose target matches v2. O(n)List<Edge> edges()
- O(m) to build (if we had an iterator, we
could use the ideas from exam 2, problem 4)List<Integer> vertices()
- O(n) to build, O(n) to visitList<Edge> edgesFrom(int vertex)
- O(1) to build, O(n) to visitVersion 3: A 2D array of weights. A[i,j] = weight of edge from i to j.
To
v1 v2 v3 v4
+---+---+---+---+
v1| | w | | w |
+---+---+---+---+
v2| | | w | |
From +---+---+---+---+
v3| | | | |
+---+---+---+---+
v4| | w | | |
+---+---+---+---+
Edge getEdge(int vFrom, int vTo)
- O(1)List<Edge> edges()
- O(n*n)List<Integer> vertices()
- O(n)List<Edge> edgesFrom(int vertex)
- O(n)Version 4: A hash table of edges (indexed by from/to pairs)
Which should we use? It depends on what algorithm we have. If our
algorithm does lots of calls to getEdge
and fewer to the other methods,
we should use the edge matrix. Most of the rest of the time, the array
of lists seems to work well.