Graph Theory
● Goals
○Explore the idea of a graph
○ Explore what kinds of questions we can ask, and
answer using graphs
3.
Graph Theory
"In mathematicsand computer science, graph theory is the
study of graphs, which are mathematical structures used to
model pairwise relations between objects. A graph in this
context is made up of vertices or nodes and lines called
edges that connect them. A graph may be undirected,
meaning that there is no distinction between the two
vertices associated with each edge, or its edges may be
directed from one vertex to another... Graphs are one of the
prime objects of study in discrete mathematics."
http://en.wikipedia.org/wiki/Graph_theory
Seven Bridges ofKönigsberg
Euler's formulation with edges, vertices, and
faces of a convex polyhedron was studied and
generalized by others and is at the origin of
topology.
Considered to be the first paper on graph
theory (1736).
The answer is: no, you can't walk them each
just once
11.
Graph Theory
● Graphscan be used to model many types of
relations and process dynamics in physical,
biological, social and information systems
● Graphs can be used to represent networks
of communication, data organization,
computational devices, the flow of
computation, the link structure of a
website, to study molecules in chemistry
and physics, friends, etc.
● Many practical (and lucrative) problems can
be represented by graphs
12.
4-Color Conjecture, nowTheorem
"No more than four colors are required to color
the regions of a map so that no two adjacent
regions have the same color."
13.
Jargon
● Path -sequence of edges
● Directed Acyclic Graph (DAG)
○ No cycles, no returns
○ One-way edges
○ Not a tree though (can get to a vertex any number of
paths)
● Undirected Graph - edges allow two-way connections
● Cyclic Graph - graph with cycles, returns to previously
visited vertices
● Adjacent Vertices - those that are one-edge away from
a vertex
● Visited - has a vertex already been visited along a
previous path?
How can wetraverse the graph?
● Start at the first vertex
a. Expand the vertex to get its adjacent vertices
b. Put those in a queue
c. pop off a vertex, go to (a)
● Start at the first vertex
a. Expand the vertex to get its adjacent vertices
b. Put those in a stack
c. pop off a vertex, go to (a)
Which DFS orBFS?
● If you are searching for an item and you use
Breadth-First Search, then you are
guaranteed to find the shortest path
● DFS is useful for maze searching
http://upload.wikimedia.org/wikipedia/commons/transcoded/4/45/MAZE_30x20_DFS.ogv/MAZE_30x20_DFS.ogv.360p.webm
What data structuresdo we need?
● A queue if we are going to traverse the
graph in breadth-first order
● A stack if we are going to traverse the graph
in depth-first order
Changes to TextAdventure
1.Need a Doubly-Linked List to implement
either stack or queue: DONE
2. Need to add a visited boolean to State: Easy
3. Need to get adjacent states: Not too hard
a. Have the connections in a Dictionary()
b. Dictionary maps String (a command "north") to
String (a state name, such as "Basement")
c. dictionary.values() are the state names
d. Game.states contains the actual state objects
e. Look up each state name in Game.states to get
state
4. Traverse the graph!
24.
Dictionary.values method
public String[ ] values() {
String [ ] retval = new String[data.size()];
int i = 0;
for (String command: data.keySet()) {
retval[i++] = data.get(command);
}
return retval;
}
25.
Traversing a GameGraph
Game game = new Game();
game.readFile("http://bubo.brynmawr.edu/~bklewis/Game.txt");
State current = game.state;
DoubleLinkList<State> queue = new DoubleLinkList<State>();
queue.pushTail(current);
while (queue.size() != 0) {
current = queue.popHead();
if (current.visited)
continue;
current.visited = true;
for (State state : current.adjacent(game.states)) {
if (!state.visited) {
queue.pushTail(state);
}
}
}