Graph Theory
Graph Theory
● Goals
○ Explore the idea of a graph
○ Explore what kinds of questions we can ask, and
answer using graphs
Graph Theory
"In mathematics and 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
Graphs
6 vertices, 7 edges, undirected
Seven Bridges of Königsberg
Seven Bridges of Königsberg
Seven Bridges of Königsberg
Can you walk all seven bridges, without
walking over the same bridge twice?
Seven Bridges of Königsberg
Seven Bridges of Königsberg
Seven Bridges of Kö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
Graph Theory
● Graphs can 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
4-Color Conjecture, now Theorem
"No more than four colors are required to color
the regions of a map so that no two adjacent
regions have the same color."
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?
DAG (Directed Acyclic Graph)
How can we traverse 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)
Breadth-First Traversal
Depth-First Traversal
Which DFS or BFS?
● 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
Breadth-First Traversal
queue.pushTail(startState);
while (queue.size() != 0) {
current = queue.popHead();
for (State state : adjacents(current))
{
queue.pushTail(state);
}
}
Depth-First Traversal
queue.pushHead(startState);
while (queue.size() != 0) {
current = queue.popHead();
for (State state : adjacents(current))
{
queue.pushHead(state);
}
}
What data structures do 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
Doubly-Linked List, fixes
public T popHead() {
if (head != null) {
Node<T> temp = head;
head = head.next;
if (head != null) {
head.prev = null;
} else {
tail = null;
}
temp.next = null;
count--;
return temp.get();
}
return null;
}
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!
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;
}
Traversing a Game Graph
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);
}
}
}

CPE-121-Discrete-Math_Graph-Theory-Introduction.pptx

  • 1.
  • 2.
    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
  • 4.
    Graphs 6 vertices, 7edges, undirected
  • 5.
    Seven Bridges ofKönigsberg
  • 6.
    Seven Bridges ofKönigsberg
  • 7.
    Seven Bridges ofKönigsberg Can you walk all seven bridges, without walking over the same bridge twice?
  • 8.
    Seven Bridges ofKönigsberg
  • 9.
    Seven Bridges ofKönigsberg
  • 10.
    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?
  • 14.
  • 15.
    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)
  • 16.
  • 17.
  • 18.
    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
  • 19.
    Breadth-First Traversal queue.pushTail(startState); while (queue.size()!= 0) { current = queue.popHead(); for (State state : adjacents(current)) { queue.pushTail(state); } }
  • 20.
    Depth-First Traversal queue.pushHead(startState); while (queue.size()!= 0) { current = queue.popHead(); for (State state : adjacents(current)) { queue.pushHead(state); } }
  • 21.
    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
  • 22.
    Doubly-Linked List, fixes publicT popHead() { if (head != null) { Node<T> temp = head; head = head.next; if (head != null) { head.prev = null; } else { tail = null; } temp.next = null; count--; return temp.get(); } return null; }
  • 23.
    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); } } }