Graph Theory
Graph Theory
Graph Theory
Graphs
• Graphs are collections of ordered pairs, where every
node may be connected to any other node in the data
structure OR to multiple (or All) other nodes
• Think of a graph like a tree without a single root node
VERTICES
Graphs…
• Graphs are also known as Networks
• Unlike a tree or binary tree, a graph does not have a
root – no primary entry point.
• Any node can be connected to any other node by edges
• Can have any number of edges and nodes
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An Edge e = (u,v) is a pair of vertices
• Example:
a b
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d), c
(b,e),(c,d),(c,e),
(d,e)}
d e
Things Graphs Are Good For
• computer networks
• airline flights
• road map
• course prerequisite structure
• tasks for completing a job
• flow of control through a program
• connecting related things (like
actors in movies)
• Electronic circuits CS16
Land Networks:
(roads, flights,
communications)
JFK
LAX STL
HNL MIA
DFW
Directed Graphs
• Some graphs are directed. They are called Digraphs
• This means that an edge goes in a particular direction from
one vertex to another vertex.
a b
a
Paths
• A PATH is a set of vertices that connect
• A SIMPLE PATH is a set of connected vertices where
every vertex (except maybe the first/last) are unique
Simple
Path
Path
(not simple)
Even More Terminology
•connected graph: any two vertices are connected by some path
d(0) = 2
d(1) = 3
d(2) = 3
d(4) = 5
…
Trees
• An undirected graph with no cycles is a tree
• Ideally, when traveling through a graph, we want to turn it
into a spanning tree – that is, a tree that passes through
every node at least once
How Do we Represent Our Graphs?
• Graphs tend to be stored in memory in two main ways
1 5
0 2
0 3 6
1 2 7
3 1
0 1 1 1 0 1 0 0 1 1 0 0 0 0 0
1 1 0
0 1 1 1 0 1 0 0 1 0 0 0
2 0 0 0
1 1 0 1 1 0 0 1 0 0 0 0
1 1 1 0 G2 0 1 1 0 0 0 0 0
G1 0 0 0 0 0 1 0 0
symmetric 0 0 0 0 1 0 1 0
undirected: n /2 2 0 0 0 0 0 1 0 1
directed: n2
G4 0 0 0 0 0 0 1 0
adjacency lists
0 0 A
A 1 B
1 4 2 C
E 3 D
B
4 E
0 1 2 4
C D 1 2
2 3
2 1 3
3
4
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G2 G3
2
Some graph operations
DFS Tree
Implementation of DFS
• Observations:
– the last node visited is the first node from which to
proceed.
– Also, the backtracking proceeds on the basis of
"last visited, first to backtrack too".
– This suggests that a stack is the proper data
structure to remember the current node and how
to backtrack.
DFS (Pseudo Code as a “Stack”)
DFS(input: Graph G) {
Stack S;
Integer x, t;
while (G has an unvisited node x){
visit(x); push(x,S);
while (S is not empty){
t := peek(S);
if (t has an unvisited neighbor y){ visit(y);
push(y,S); }
else
pop(S);
}
}
}
Breadth-First Search
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes
from this level form a new level that becomes the
next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
Illustration of BFS
0 1
0
2 4 2
1
9 4
5 9 10
10 5 7
11 8
6 7 8 11
6
BFS Tree Graph G
32
Implementation of BFS
• Observations:
– the first node visited in each level is the first node
from which to proceed to visit new nodes.
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){
visit(y); Enqueue(y,Q);
}
}
}
}
The Primary Difference between BFS
and DFS
• Is the idea of stacking vs. queuing.