Graphs: University Institute of Engineering (UIE)
Graphs: University Institute of Engineering (UIE)
Graphs: University Institute of Engineering (UIE)
GRAPHS
Graphs
Definition of Graphs and Related
Concepts
Representation of Graphs
The Graph Class
Graph Traversal
Graph Applications
Definition of Graphs
A graph is a finite set of nodes with edges between nodes
Formally, a graph G is a structure (V,E) consisting of
a finite set V called the set of nodes, and
a set E that is a subset of VxV. That is, E is a set of pairs of the form
(x,y) where x and y are nodes in V
Examples of Graphs
V={0,1,2,3,4}
E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
4
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Mary Helen
John Joe
Tom Paul
Graph Representation
Adjacency Matrix
Representation
In this representation, each graph of n nodes is
represented by an n x n matrix A, that is, a two-
dimensional array A
The nodes are (re)-labeled 1,2,,n
A[i][j] = 1 if (i,j) is an edge
A[i][j] = 0 if (i,j) is not an edge
1
0
0 1 0 1 0
0 0 1 0 0
A= 0 0 1 0 0 2
1 0 0 0 0
0 0 0 4 0 4
3
Example of Linked
Representation
L[0]: empty Mary 0 Helen 1
L[1]: empty
Joe
L[2]: 0, 1, 4, 5 John 2 3
L[3]: 0, 1, 4, 5
L[4]: 0, 1 Tom 4 Paul 5
L[5]: 0, 1
class Graph {
public:
typedef int datatype;
typedef datatype * datatypeptr;
Graph( int n=0); // creates a graph of n nodes and no edges
bool isEdge( int i, int j);
void setEdge( int i, int j, datatype x);
int getNumberOfNodes(){return numberOfNodes;};
private:
datatypeptr *p; //a 2-D array, i.e., an adjacency matrix
int numberOfNodes;
};
Mary Helen
if (x,y) is an edge:
we say that x is
John Joe adjacent to y, &
y adjacent to x.
We also say that
x and y are
Tom Paul neighbors
Representations of Undirected
Graphs
The same two representations for directed graphs can be
used for undirected graphs
Adjacency matrix A:
A[i][j]=1 if (i,j) is an edge; 0 otherwise
Adjacency Lists:
L[i] is the linked list containing all the neighbors of i
Example of Representations
Mary 0 Helen 1
Linked Lists:
L[0]: 1, 2, 3 John 2 Joe 3
L[1]: 0, 2, 3
Tom 4 Paul 5
L[2]: 0, 1, 3
Adjacency Matrix:
L[3]: 0, 1, 2
L[4]: 5 0 1 1 1 0 0
1 0 1 1 0 0
L[5]: 4 A= 1 1 0 1 0 0
1 1 1 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
Paths
A path in a graph G is a sequence of nodes
x1, x2, ,xk, such that there is an edge from
each node the next one in the sequence
For example, in the first example graph, the
sequence 3, 0, 1, 2 is a path, but the
sequence 0, 3, 4 is not a path because (0,3)
is not an edge
In the sibling-of graph, the sequence John,
Mary, Joe, Helen is a path, but the sequence Helen,
Tom, Paul is not a path
Graph Connectivity
An undirected graph is said to be connected if there is a
path between every pair of nodes. Otherwise, the graph
is disconnected
Informally, an undirected graph is connected if it hangs
in one piece
Disconnected Connected
Connected Components
If an undirected graph is not connected, then
each piece is called a connected component.
A piece in itself is connected, but if you bring any other
node to it from the graph, it is no longer connected
If the graph is connected, then the whole graph
is one single connected component
Of Interest: Given any undirected graph G,
Is G connected?
If not, find its connected components.
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 the 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.
We will redo the DFS on the previous graph, but this time
with stacks
In Class
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
Implementation of DFS
Observations:
the first node visited in each level is the first node from which to
proceed to visit new nodes.
We will redo the BFS on the previous graph, but this time
with queues
In Class