CS100-Graph1.pptx

advertisement
An Assortment of Things
1. This class will go over the exam. If you haven’t taken the
exam yet (like maybe you forgot and didn’t show up) please
don’t attend this class.
2. Please get in groups of 3-4 people (4 is better…1 and 2 are
NOT ok)
The Plan for Today
1. A Musical Interlude
2. We review a few key questions from the
exam
a. whoWinsPilesList (Recursive backtracking)
b. getMissingParens (Stacks)
3. I introduce “Graphs” the final structure we
will look at
4. Hand back exams
The Music Video Project
1. A student created CompSci 100 tradition
2. Create a music video, recorded song, or just a
set of lyrics about some part of CompSci 100.
3. Work in groups
4. Your creations will be played as folks arrive
for the final exam
5. Extra credit (but that’s not really the point)
Work on teams of 3-4 to Solve
whoWinsPilesList
• Do not work by yourself or with a partner
• Snarf the code and check your solution as you
develop it
• There is a hint at the bottom of the textfile, if
everyone on you team really is stuck
• When you finish, more on to getMissingParens
• Make sure everyone on your team understands
how this code works
• When you are finished, submit your code via
ambient
Graphs
• “Nodes” with edges between them
B
•Could be “cyclic” or “acyclic”
•Could be “directed” or
“undirected”
A
D
C
Might represents a map, where nodes represent cities and
edges represent roads
Represent the graph below with all 3
different mechanisms
A Node Class…similar to the way we do with linked lists and trees
class Node {
public String name;
public ArrayList<Node> neighbors;
}
A adjacency list, where each node ‘name’ maps to a list of neighbors
HashMap<String,ArrayList<String>> graph;
//is there an edge from a to b?
boolean edgeExists = graph.get(“A”).contains(“B”);
A adjacency matrix, where connections between nodes is represented by a 2D matrix
boolean [][] graph;
//is there an edge from 3 to 17?
boolean edgeExists = graph[3][17];
1
0
2
Can even be implicit
You can move between two
words if the last letter of the
previous word matches the
first letter of the next word
grand
dog
dad
Download