0% found this document useful (0 votes)
45 views40 pages

Graph - Representation

The document discusses graph terminology and representations. It defines graphs and their components such as vertices, edges, paths, cycles, and degrees. It also explains adjacency matrices and lists which are common ways to represent graphs in computers. Finally, it covers graph traversal techniques like breadth-first search and depth-first search.

Uploaded by

Iftakhar Utsho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views40 pages

Graph - Representation

The document discusses graph terminology and representations. It defines graphs and their components such as vertices, edges, paths, cycles, and degrees. It also explains adjacency matrices and lists which are common ways to represent graphs in computers. Finally, it covers graph traversal techniques like breadth-first search and depth-first search.

Uploaded by

Iftakhar Utsho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Algorithms

Graphs basic & Traversal Techniques

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

• A directed graph or digraph is a graph in


which each line has a direction associated
with it.
• In a directed graph, the flow along an edge
between 2 vertices can only follow the
indicated direction.

5
Terminology

6
Terminology
• 2 vertices in a graph are said to be adjacent (or
neighbors) if an edge directly connects them.

A and B are adjacent


D and F are not adjacent

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

A,B,E,C is NOT a path


because, although E
is adjacent to C,
C is not adjacent
to E (note direction). 8
Terminology
• Note that both directed and undirected graphs
have paths.
• In an undirected graph, you may travel in
either direction.
• In a directed graph, you may only travel along
the given direction of the arcs.

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

• A self edge or loop is an arc that begins and


ends at the same vertex.

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

• The degree of vertex B is … 3


• The outdegree of vertex B is … 2
• The indegree of vertex B is … 1

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

• 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the


graph

• Each row and column is indexed by the vertex id


– e,g a=0, b=1, c=2, d=3, e=4

• A[i][j]=1 if there is an edge connecting vertices i and j;


otherwise, A[i][j]=0
Simple Questions on Adjacency Matrix

• Is there a direct link between A and B?


• What is the indegree and outdegree for a
vertex A?
• How many nodes are directly connected to
vertex A?
• Is it an undirected graph or directed graph?
• Suppose ADJ is an NxN matrix. What will be
the result if we create another matrix ADJ2
where ADJ2=ADJxADJ?
Adjacency List

• If the graph is not dense, in other words, sparse, a better


solution is an adjacency list
• The adjacency list is an array A[0..n-1] of lists, where n is
the number of vertices in the graph.
• Each array entry is indexed by the vertex id
• Each list A[i] stores the ids of the vertices adjacent to
vertex i
Adjacency Matrix Example

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

• Here we see that the breadth-first traversal starts at


level 0 and then processes all the vertices in level 1
before going on to process the vertices in level 2.
25
Breadth-First Traversal
1. We begin by
enqueuing the 1st
vertex, A.

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.

The vertex vector contains an


entry for each node in the
graph.

The adjacency matrix contains


a ‘1’ at the appropriate
intersection to represent an
edge.

36
Adjacency Matrix
For example, A and B are
adjacent in the graph in (a).

Hence, at the intersection


of A and B in the adjacency
matrix, we place a ‘1’ to
denote an edge.

Similarly, for the digraph


shown, we place a ‘1’ at the
intersection of A and B.
However, because this is a
digraph, there is only one ‘1’.
37
Adjacency Matrix
The intersection at which the
‘1’ appears is determined by
the direction of the arc
between adjacent nodes of
the digraph.

It doesn’t matter which


intersection gets the ‘1’ – just
remain consistent.

I like the notation:


matrix[i][j] = 1 if there is an
edge from vertex i to vertex j
38
Adjacency List
• If the graph structure is unknown ahead of
time, we can use linked lists to implement the
vertex vector and adjacency matrix.
• In this case, they are referred to as a vertex list
and an adjacency list.

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

You might also like