Graph - Representation
Graph - Representation
1
Terminology
• A graph is a collection of nodes, called
vertices, and a collection of line segments,
called lines, edges, or arcs, connecting pairs of
vertices.
• In other words, a graph consists of 2 sets:
– A set of lines
– A set of vertices
2
Definition
• A graph G=(V, E) consists a set of vertices, V, and a set of edges,
E.
• Each edge is a pair of (v, w), where v, w belongs to V
• If the pair is unordered, the graph is undirected; otherwise it is
directed
{a,b} {a,c}
{b,d} {c,d}
{b,e} {c,f}
{e,f}
An undirected graph
Terminology
• Graphs may be either directed or undirected.
• An undirected graph is a graph in which
there is no direction associated with the
lines.
• In an undirected graph, the flow between
two vertices can go in either direction.
4
Terminology
5
Terminology
6
Terminology
• 2 vertices in a graph are said to be adjacent (or
neighbors) if an edge directly connects them.
7
Terminology
• A path is a sequence of vertices in which each
vertex is adjacent to the next one.
A,B,C,E is a path
A,B,E,F is a path
9
Terminology
• A cycle is a path that starts and ends with the
same vertex.
• Note: a single vertex is not a cycle.
B, C, D, E, B is a
cycle in the the
undirected graph
but not in the
directed graph.
10
Terminology
11
Terminology
• Two vertices are said to be connected if there
is a path between them.
• A directed graph is strongly connected if there
is a path from each vertex to every other
vertex.
• A directed graph is weakly connected if at
least 2 vertices are not connected.
• A graph is disconnected or disjoint if it is not
connected.
12
Terminology
13
Terminology
• The degree of a vertex is the number of edges
incident to it.
• The outdegree of a vertex in a digraph is the
number of arcs leaving the vertex.
• The indegree of a vertex in a digraph is the
number of arcs entering the vertex.
14
Terminology
15
Graph Representation
• Two popular computer representations of a
graph. Both represent the vertex set and
the edge set, but in different ways.
1. Adjacency Matrix
Use a 2D matrix to represent the graph
2. Adjacency List
Use a 1D array of linked lists
Adjacency Matrix
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
8
1 0 0 1 1 0 0 0 1 0 1
2 9 2 0 1 0 0 1 0 0 0 1 0
1 3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
3 7
6 5 0 0 0 1 0 0 1 0 0 0
4 6 0 0 0 0 0 1 0 1 0 0
5
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Adjacency List Example
0 8
0
1 2 3 7 9
8
2 1 4 8
2 9 3 1 4 5
1 4 2 3
5 3 6
3 7
6 5 7
6
4 7 1 6
5
8 0 2 9
9 1 8
Adjacency List vs. Matrix
• Adjacency List
– More compact than adjacency matrices if graph has few
edges
– Requires more time to find if an edge exists
• Adjacency Matrix
– Always require n2 space
• This can waste a lot of space if the number of edges are
sparse
– Can quickly find if an edge exists
Graph Traversal
• Application example
– Given a graph representation and a vertex s in the
graph
– Find paths from s to other vertices
• Two common graph traversal algorithms
• Breadth-First Search (BFS)
– Find the shortest paths in an unweighted graph
• Depth-First Search (DFS)
– Topological sort
– Find strongly connected components
Breadth-First Traversal
• In the breadth-first traversal of a graph, we
process all adjacent vertices of a vertex before
going to the next level.
• We also saw the breadth-first traversal of a
tree.
24
Breadth-First Traversal
26
Breadth-First Traversal
2. We then loop,
dequeuing the queue,
and, after processing
the vertex at the front
of the queue, enqueue
all of its adjacent
vertices. To process X
at Step 2, therefore,
we dequeue X, process
it, and then enqueue
G and H.
27
Breadth-First Traversal
3. When the queue is
empty, the traversal
is complete.
28
Depth-First Traversal
• In the depth-first traversal, we process all of a
vertex’s descendants before we move to an
adjacent vertex.
• This concept is most easily seen when the
graph is a tree.
29
Depth-First Traversal
• Here we show the preorder traversal, one of
the standard depth-first traversals.
30
Depth-First Traversal
1. We begin by
pushing the 1st vertex,
A, onto the stack.
31
Depth-First Traversal
2. We then loop, pop
the stack, and, after
processing the vertex,
push all of its adjacent
vertices onto the stack.
To process X at Step 2,
therefore, we pop X
from the stack, process
it, and
then push G and H
onto the stack.
32
Depth-First Traversal
3. When the stack is
empty, the traversal
is complete.
33
Adjacency Matrix
• If the graph is directed, then the intersection
chosen in the adjacency matrix indicates the
direction.
34
Adjacency Matrix
• |V| |V| matrix A.
• Number vertices from 1 to |V| in some arbitrary manner.
• A is then given by: 1 if (i, j ) E
A[i, j ] aij
1
0 otherwise
2 1 2 3 4
a b
1 0 1 1 1
2 0 0 1 0
c d4 3 0 0 0 1
3 4 0 0 0 0
1 2 1 2 3 4
a b
1 0 1 1 1
2 1 0 1 0
c d 3 1 1 0 1
3 4 4 1 0 1 0
35
35
Adjacency Matrix
Here we see the vertex vectors
and the adjacency matrices
for the graphs shown.
36
Adjacency Matrix
For example, A and B are
adjacent in the graph in (a).
39
Adjacency List
Here, we have a linked list of the nodes in the graph. Each node
in the graph contains a pointer to the head of an adjacency list.
The adjacency list is a linked list containing adjacent nodes. 40