Uploaded by ChanHo Park

COMP2123A4

advertisement
COMP2123 Programming Technologies and Tools
Assignment 4
Due: 17:30 16 May, 2019
* Please submit your answer through Moodle VPL
* Please contact TA Bai Ge ([email protected]) and cc Kit ([email protected]) if you got any questions
regarding this assignment :-)
In this question, we will solve the problem in Assignment 3 Part 2 again using C language.
Generally speaking, there are two ways to store the directed graph: adjacency matrix (Fig 2)
and adjacency list (Fig 3). Adjacency matrix M is a square (0,1)-matrix where each element
Mij indicates whether vertices i and j are adjacent or not in the graph. Adjacency list is a
two-layer link list. It represents each row i of the adjacency matrix as a link list i. The nodes
in link list i represent the vertices that are adjacent from vertex i. Adjacency list is used to
store the directed graph in this question.
Figure 1: A directed graph with 6 nodes and 12 directed edges (a bidirectional edge is regarded
as two directed edges in this question, so there are 12 directed edges in total).
Figure 2: Adjacency matrix.
1
Figure 3: Adjacency List.
An Adjacency List struct is defined as follows (you can find the incomplete code in
another file):
typedef struct _Node{
char name[100];
int id;
struct _Node *next;
} Node;
typedef struct _Adj_Row{
Node *header;
struct _Adj_Row *next;
} Adj_Row;
typedef struct{
Adj_Row *header;
} Adj_List;
The main function is as follows:
int main(){
char command[100];
Adj_List g;
g.header=NULL;
while (scanf("%s", command)){
if (strcmp(command, "InsertNode")==0){
int x;
char name[100];
scanf("%d %s", &x, name);
InsertNode(&g, x, name);
}
else if (strcmp(command, "InsertEdge")==0){
int x, y;
scanf("%d %d", &x, &y);
InsertEdge(&g, x, y);
}
else if (strcmp(command, "DeleteNode")==0){
2
int x;
scanf("%d",&x);
DeleteNode(&g, x);
}
else if (strcmp(command, "DeleteEdge")==0){
int x,y;
scanf("%d %d",&x, &y);
DeleteEdge(&g, x, y);
}
else if (strcmp(command, "PrintAdjacencyList")==0){
PrintAdjacencyList(&g);
}
else if (strcmp(command, "Exit")==0){
return 0;
}
else
printf("No such command\n");
}
return 0;
}
Please implement five commands: InsertNode, InsertEgde, DeleteEgde, DeleteNode and
PrintAdjacencyList.
1. InsertNode x y - Insert the node with id as x and name as y in a graph. For example,
we issue the following commands to insert 6 nodes in the graph in Figure 1.
InsertNode
InsertNode
InsertNode
InsertNode
InsertNode
InsertNode
0
1
2
3
4
5
Central
Kowloon
Lai_King
Tsim_Sha_Tsui
Mong_Kok
Wong_Tai_Sin
• You can assume that the name of the nodes will not contain any space and length of
name doesn’t exceed 100.
• The id of the nodes is an integer which may not start from 0 and may not be in
ascending order.
• If the id of the node has been existed already, output “Duplicated node.” on screen.
2. InsertEdge x y - Insert an edge x→y in the graph, where x and y are the id of nodes. For
example, we issue the following commands to insert the 12 edges in the graph in Figure
1.
InsertEdge
InsertEdge
InsertEdge
InsertEdge
InsertEdge
InsertEdge
InsertEdge
0
1
1
2
0
3
2
1
0
2
1
3
0
4
3
InsertEdge
InsertEdge
InsertEdge
InsertEdge
InsertEdge
4
3
4
4
5
2
4
3
5
4
• If there are no nodes in the Graph with id equal to x or y, output “No such node.”
on screen.
• If there has existed an edge from id x to id y already, do nothing but output “Existed
edge.” on screen.
3. DeleteEdge x y - delete an edge x→y in the graph, where x and y are the id of nodes.
DeleteEdge
DeleteEdge
DeleteEdge
DeleteEdge
0
1
1
5
1
0
2
4
• If there isn’t an edge from id x to id y, output “No such edge.” on screen.
4. DeleteNode x - delete a node with id x in the graph. Notice that we need to delete all
information about node x, including the node x and all edges adjacent to x.
DeleteNode 4
• If there isn’t a node in the Graph with id equal to x, output “No such node.” on
screen.
5. PrintAdjacencyList - print the structure of adjacency List. You are supposed to print
each name of node with ascending order firstly. And for each node with id x, print the
node with id y if there exists an edge from node id x to node id y. Please notice you
also need to print node with id y in ascending order in each row. For example, you are
supposed to print something as the following for Fig 1.
Central: Kowloon - Tsim_Sha_Tsui
Kowloon: Central - Lai_King
Lai_King: Kowloon - Mong_Kok
Tsim_Sha_Tsui: Central - Mong_Kok
Mong_Kok: Lai_King - Tsim_Sha_Tsui - Wong_Tai_Sin
Wong_Tai_Sin: Mong_Kok
To make it easy, we’ll give our program about PrintAdjacencyList ,. You can use it to
print Adjacency List, or you can implement your version. Note that the following program
does not sort the adjacency list, so to use this, you need to make sure that the adjacency
list is already sorted.
void PrintAdjacencyList(Adj_List *g){
Adj_Row *p=g->header;
while (p!=NULL){
Node *p_Node=p->header;
printf("%s",p_Node->name);
4
p_Node=p_Node->next;
int flag=0;
while(p_Node!=NULL){
if (!flag){
printf(": %s",p_Node->name);
flag=1;
}
else
printf(" - %s",p_Node->name);
p_Node=p_Node->next;
}
printf("\n");
p=p->next;
}
}
You can see the sample cases in another file.
Complete your implementation named AdjList.c and submit it through Moodle.
–END–
5
Download
Random flashcards
Radiobiology

39 Cards

Marketing

46 Cards

History of Europe

27 Cards

Create flashcards