You are on page 1of 35

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

Besides mathematical usages,


graphs are important for map
software and any other
software that must maintain
multiple relationships between
many nodes or points of data
GRAPHS
• A Graph is made up of related vertices and edges
• Vertex (plural is Vertices): What nodes in a graph are
called
• Edge: The connection between Vertices is an edge
EDGE  Technically, trees are
graphs
a b
 But Graphs are not
necessarily TREES!

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.

 Endless loops in a graph are called cycles.


 Technically, all edges in an undirected loop is a cycle
Directed VS Undirected
• Undirected graph (graph)
– There can be at MOST, One edge between two
vertices
– Edges do not have a direction
• (V1, V2) and (V2, V1) are the same edge
• Directed graph (digraph)
– There can be at MOST, Two edges between two
vertices (one in each direction).
– Edges have a direction
• <V1, V2> and <V2, V1> are different edges
• For either type, edges may be weighted or
unweighted
Weighted Graphs
• Sometimes we
gives edges weight

• This is a number (or


other piece of data)
attached to an edge to
give it extra
significance
• If you are using a
graph to draw a map,
the “weight” of an
edge, might be
distance.
More Terminology
• Vertices a and b are adjacent if there is an edge
between them

• Similarly, the edge (a,b) is incident on a and b


Incident on (a,b)

a b

Adjacent to each other


Loops
• A Graph can have no Loops
• This is when a vertex has an edge to itself (a,a)

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

connected not connected


• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g., the graph
below has 3 connected components.
Degrees
• Degree di is the number of edges a vertex i has

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

• Adjacency Matrix: This is a matrix (in this case, a two-


dimensional array) of size n*n
– If there is an edge between nodes 2 and 5 in a 6*6 matrix,
then position graph[2][5] has a value of 1. In an
undirected graph, position graph[5][2] also is 1
– All locations without edges have the value 0
• Adjacency List: This represents the graph as a list
of lists
– The primary list contains all vertices, regardless of
their connection
– Each vertex contains another list of all of its edges
Graph data structures
• Storing the vertices
– Each vertex has a unique identifier and, maybe, other
information
– For efficiency, associate each vertex with a number
that can be used as an index – more important with
the adjacency matrix
• Storing the edges
– adjacency matrix – represent all possible edges
– adjacency lists – represent only the existing edges
Matrices and Lists

An undirected graph and its adjacency matrix representation.

An undirected graph and its adjacency list representation.


Storing the vertices
• When a vertex is added to the graph, assign it a
number or identifier
– vertices are numbered between 0 and n-1 in an
adjacency matrix
• Graph operations start by looking up the
number associated with a vertex
• many data structures to use
– any of the associative data structures
– for small graphs a vector can be used
• search will be O(n)
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional

n by n array, say adj_mat


If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Examples for Adjacency Matrix 0 4

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

adjacency matrix adjacency lists


insertEdge O(1) O(e)

isEdge O(1) O(e)

#successors? O(V) O(e)

#predecessors? O(V) O(E)

Memory space used?


How do We Traverse the Graph
• There are two primary ways that we walk through
graphs:
1. Breadth-first Search (BFS)
2. Depth-first Search (DFS)

• In both DFS and BFS, the nodes of the undirected


graph are visited in a systematic manner so that every
node is visited exactly one.
• Both BFS and DFS create a tree:
– When a node x is visited, it is labeled as visited, and it is
added to the tree
– If the traversal got to node x from node y, y is viewed as
the parent of x, and x a child of y
Depth-First Search

DFS follows the following rules:


1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit
it, and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
Illustration of DFS
0 1
0
2
1
9 4
4
10 5 7
2
11 8
5 9
6
7 11
6 Graph G
8 10

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.

• This suggests that a queue is the proper data


structure to remember the order of the steps.

• Though, like with DFS, any list-like structure


should work
BFS (Pseudo Code)

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.

• With DFS, you act on each new node added to


the stack immediately (LIFO)

• With BFS, you add each new node to the end of


the queue and then process the oldest one still
in the queue (FIFO)

You might also like