Graph Problem

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Graph Representation: A1

Graph. A graph 0 consists of a set of nodes I = {:


1
, :
2
, , :
n
] and a set of edges E where (:

, :
]
) E
when there is a connection between vertices :

and :
]
. This connection may mean a road segment, a
friendship, or any other relationship between the two vertices. Edges can be directional (directed graph)
and unidirectional (undirected graph). Figure 1 shows an undirected graph consisting of 6 vertices and 7
edges.

Figure 1: An example of a graph.
Representation of graphs in programs. In a program, a graph is usually represented in any of the two
ways: (i) by an adjacency matrix or (ii) by an adjacency list.
Adjacency matrix. In this representation, a two-dimensional matrix A of size n X n is used to store the
graph. Each element o
]
stores the presence of absence of an edge between vertices :

and :
]
. Whenever
(:

, :
]
) E, o
]
= 1 otherwise o
]
= u. If the graph is undirected, then o
]
= o
]
. An array data structure
can be used to simulate the matrix in programming languages. The following shows the adjacency matrix
representation of graph of Fig. 1.
A =
u 1 u
1 u 1
u 1 u
u 1 u
u 1 u
1 u u
u u 1
1 1 u
u u u
u 1 1
1 u u
1 u u

Adjacency list. In this representation there are n lists: I
1
, I
2
, , I
n
. Each list I

is a list of vertices that are


adjacent to vertex :

. Each of these lists can be implemented by a linked list data structure in


programming languages. The following shows the adjacency list representation of the graph of Fig. 1.
I
1
=< 2, S >
I
2
=< 1, S, S >
I
3
=< 2, 4 >
I
4
=< S, S, 6 >
I
5
=< 1, 2, 4 >
I
6
=< 4 >

Graph Representation: A1


Programming Tasks for today
Task 1. Input a graph from the user, and store it in adjacency matrix form. Assume that user will give
input in the following form (input resembles the graph of Fig. 1).
6 5
1 2
1 5
2 3
2 5
3 4
4 5
4 6
Explanation of input: The two integers in the first line represent the number of vertices and edges in the
graph. Each of the next lines (except the last line) gives two integers i and ] who have edges between
them in the graph. Note that, for an undirected graph input, edge is given for one direction only: (i, ]).
The other direction (], i) is to be assumed. The last line (0 0) ends the input.
Task 2. After taking input from user, you will ask the user to give some pairs of vertices. You will output
whether those pairs have an edge between them. If there is an edge, output YES, otherwise output
NO. A sample input sequence is given below.
1 5
2 6
4 6
3 5
0 0
Explanation of input: Each line gives two integers i and ]. The last line (0 0) ends the input. The sample
output should be as follows (for verification, see Fig. 1).
YES
NO
YES
NO
Task 3. Ask the use for a sequence of vertices. You will print the neighbor vertices and the degree of each
vertex. The degree of a vertex is the number of edges, i.e., neighbors of that vertex. A sample input
sequence is given below.
Graph Representation: A1


5
6
1
0
Explanation of input: Each line gives an integer i. The last line (0) ends the input. The sample output
should be as follows (for verification, see Fig. 1). Sample output for the given sample input is as follows.
2 5: 3
4: 1
2 5: 2
Task 4. In this case, again ask the user for pairs of integers. You will output whether the pairs have a
connection between them by following exactly two edges. If there is a connection, output YES,
otherwise output NO. A sample input sequence is given below.
1 5
2 6
4 6
3 5
0 0
Explanation of input: Each line gives two integers i and ]. The last line (0 0) ends the input. The sample
output should be as follows (for verification, see Fig. 1).
YES
NO
NO
YES
Task 5. In this case, you dont require taking any input from user. You will determine whether the given
graph (in Task 1) is a 2-Connected graph. A graph G is 2-Connected if there exists a connection of two
edges between every pair of vertices. You will output YES, if the given graph is 2-Connected,
otherwise NO. For our sample graph, the output will be NO.
Programming Task for next day
This will be your home assignment. You will solve Tasks 1-5 given above. However, this time you will
store the graph using an adjacency list representation. You will need to implement Linked lists for this
assignment. You cannot use Standard Template Library (STL) or any kind of library from outside. You
have to implement the linked list yourself. In addition, you will need to solve Task 6-8 given below.
Graph Representation: A1


Task 6. In this case, you will need to determine whether the graph is 3-connected, i.e., for every pair of
vertices there exists a connection by following exactly 3 edges.
Task 7 (Bonus). Say, a graph is k-connected. Your job is to find maximum value of k. For example, for
our sample graph in Figure 1, k = u. Try to write an efficient algorithm.
Task 8. Analyze each of the algorithms you write to solve the Tasks 1-7 and find their running times in
Big-Oh notation. Then, compare the running times for the two representations: (i) adjacency matrix and
(ii) adjacency list.

You might also like