0
/ \
1 2
\ /
3
|
4
/ \
5 6
|
int main()
/* depth first and breadth first search of a graph */
{
int i;
node_pointer graph[MAX_NODES];
int n = 0; /* number of vertices currently in use */
int choice = print_menu();
while (choice !=5) {
switch(choice) {
case 1: create_list(graph,&n);
break;
case 2: printf("The list has %d vertices in use.\n",n);
print_list(graph, n);
break;
case 3: start_depth_first(graph,n);
break;
case 4: breadth_first_search(0, graph,n);
}
printf("\n\n");
choice = print_menu();
}
}
|
The code that implements the initialization is called start_depth_first(). This function requires parameters representing the graph and the number
of nodes in the graph. The code is found in the following table.
void start_depth_first(node_pointer graph[],int size)
{
int visited[MAX_NODES];
int i;
for (i = 0; i < size; i++)
visited[i] = FALSE;
printf("Depth first search: ");
depth_first_search(0, graph, visited);
} |
void depth_first_search(int node1, node_pointer graph[], int visited[])
{
/* depth first traversal of a graph beginning with node1. */
node_pointer node2;
visited[node1] = TRUE;
printf("%5d",node1);
for (node2 = graph[node1]; node2; node2 = node2->link)
if (!visited[node2->vertex])
depth_first_search(node2->vertex, graph, visited);
}
|
Enter your choice: 3 Depth first search: 0 2 3 4 6 5 1 |
vertex Adjacency List
0 2 1
1 3 0
2 3 0
3 4 2 1
4 6 5 3
5 4
6 4
|
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^4 | 6 | 5 3 |
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^6 | NONE 4 visited | |
| ^4 | 6 | 5 3 |
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^4 | 5 | 3 |
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^5 | NONE | |
| ^4 | 5 | 5 3 |
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^4 | 5 | 3 |
| ^3 | 4 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^3 | 1 | 2 1 |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
| Stack | Selected Vertex | Adjacency List |
|---|---|---|
| ^1 | NONE | 3 0 |
| ^3 | 1 | |
| ^2 | 3 | 0 |
| ^0 | 2 | 1 |
0
|
2
|
3
/ \
4 1
/ \
6 5
|
|
|
|
void breadth_first_search(int node1, node_pointer graph[], int size)
{
int i;
int visited [MAX_NODES];
node_pointer node2;
queue_pointer front,rear;
for (i=0; i < size; i++)
visited[i] = FALSE;
printf("\n\nBreadth first search:");
front = rear = NULL; /* initialize queue */
printf("%5d",node1);
visited[node1] = TRUE;
add_queue(&front,&rear,node1);
while (front) {
node1 = delete_queue(&front);
for (node2 = graph[node1]; node2; node2 = node2->link)
if (!visited[node2->vertex]) {
printf("%5d", node2->vertex);
add_queue(&front,&rear,node2->vertex);
visited[node2->vertex] = TRUE;
}
}
}
|
Enter your choice: 4 Breadth first search: 0 2 1 3 4 6 5 |
0
/ \
2 1
|
3
|
4
/ \
6 5
|
|
|
|