0% found this document useful (0 votes)
10 views6 pages

CSD201 Repair Practical Test Graph

excersice

Uploaded by

Hiếu Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

CSD201 Repair Practical Test Graph

excersice

Uploaded by

Hiếu Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

BFS traversing
Write a program to build an undirected graph by giving edge list.
Giving a start vertex, your task is showing the BFS (Breadth First Traversal) traversing of the
undirected graph.
For example,
 Giving the edge list that used to represent undirected graph as follow:
8 10 0
0 1
0 2
0 5
1 6
2 3
2 4
3 4
4 5
4 6
5 7
Figure 1. The undirected graph that created by giving adjacency matrix

 The BFS traversing of the graph is: 0, 1, 2, 5, 6, 3, 4, 7

Figure 2. The BFS traversing of the graph is: 0, 1, 2, 5, 6, 3, 4, 7

The input: are stored in the bfs_input.txt text file:


 The first line contains a 3 integers including:
 N (1 ≤ N ≤ 30) is the number of vertex.
 M (1 ≤ M ≤ 435) is the number of edge.
 S (0 ≤ S ≤ N-1) is the starting vertex.
 The next M line, each line contains 2 integers u and v that represent the edge (u, v) of the graph.
The output: the results need to be saved to the bfs_output.txt text file:
One line contains the list of numbers representing the BFS traversing of the graph. Each number
separated by one comma.
Sample Input 1 Sample Output 1

8 10 0 0,1,2,5,6,3,4,7

0 1
0 2
0 5
1 6
2 3
2 4
3 4
4 5
4 6
5 7

Sample Input 2 Sample Output 2


6 7 3 3,2,4,0,5,1
0 1
0 2
0 5
2 3
2 4
3 4
4 5
2. Minimum Span Tree (MST)
Write a program to build an undirected graph by giving adjacency matrix.
Your task is finding and showing all edge of the MST of the given undirected graph.
For example,
 Giving the adjacency matrix that used to represent undirected graph as follow:

8
0 5 3 0 0 11 0 0
5 0 5 0 0 0 7 11
3 5 0 11 1 7 0 0
0 0 11 0 11 0 11 0
0 0 1 11 0 1 5 7
11 0 7 0 1 0 0 42
0 7 0 11 5 0 0 42
0 11 0 0 7 42 42 0

Figure 3. The undirected graph that created by giving adjacency matrix

 The MST is shown as figure 4 and the weight of minimum spanning tree is 33:

Figure 4. The weight of minimum spanning tree is 33


The input: are stored in the mst_input.txt text file:
The first line contains a positive integer N (1 ≤ N ≤ 100) which is the number of vertex of undirected
graph.
The next N line, each line containing N integers that represent the adjacency matrix.
The output: the results need to be saved to the mst_output.txt text file:
One line contains the weight of minimum spanning tree.
Sample Input 1 Sample Output 1
8 33
0 5 3 0 0 11 0 0
5 0 5 0 0 0 7 11
3 5 0 11 1 7 0 0
0 0 11 0 11 0 11 0
0 0 1 11 0 1 5 7
11 0 7 0 1 0 0 42
0 7 0 11 5 0 0 42
0 11 0 0 7 42 42 0

Sample Input 2 Sample Output 2


6 18
0 7 5 0 0 1
7 0 0 0 0 0
5 0 0 2 3 0
0 0 2 0 7 0
0 0 3 7 0 9
1 0 0 0 9 0
3. Isolated vertices counting
Write a program to build an undirected graph by giving adjacency matrix.
An isolated vertex is a vertex with degree zero; that is, a vertex that is not an endpoint of any edge.
Your task is showing all isolated vertices of the given graph.
For example,
 Giving the adjacency matrix that used to represent undirected graph as follow:
9
0 1 1 0 0 1 0 0 0
1 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 0
0 0 1 0 1 0 0 0 0
0 0 1 1 0 1 0 1 0
1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0

Figure 5. The undirected graph that created by giving adjacency matrix

 All isolated vertices of the matrix are 6 and 8.


The input: are stored in the isolatedVertices_input.txt text file:
 The first line contains a positive integer N (1 ≤ N ≤ 100) which is the number of vertex of
undirected graph.
 The next N line, each line containing N integers that represent the adjacency matrix.
The output: the results need to be saved to the isolatedVertices_output.txt text file:
 One line contains the list of isolated vertices of the undirected graph, each number separated by
one comma.
 If the undirected graph doesn’t have any isolated vertex, the result file will contain the message
“There is a connected graph”.

Sample Input 1 Sample Output 1


9 6,8
0 1 1 0 0 1 0 0 0
1 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 0
0 0 1 0 1 0 0 0 0
0 0 1 1 0 1 0 1 0
1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0
Sample Input 2 Sample Output 2
6 There is a connected graph
0 1 1 0 0 1
1 0 0 0 0 0
1 0 0 1 1 0
0 0 1 0 1 0
0 0 1 1 0 1
1 0 0 0 1 0

You might also like