Programing Report DFS
Programing Report DFS
Programming Report
Ahnaf Faqih Shaimy (1113011466)
Kanazawa University
June 22, 2012
Given a maze:
Write a program to solve the maze using Depth First Search (DFS) !
Answer
Simplify problem
From Figure 1, we want to find a path from start point to end point. First, assume all dots
that make a path as vertices. To simplify this, we can make numbers for all vertices (Figure 2)
or make it as a tree (Figure 3). Here we have 121 vertices.
DFS Steps
Step 1. Define N as the numbers of all vertices. Here we have N = 121.
Step 2. Define an array with size |N |, said mark[ ]. This mark has 3 signs:
1. Unvisited : when a vertex hasnt visited yet.
2. Visited : when a vertex has visited once.
3. Blocked : when a vertex already visited twice.
Step 3. Define a 2-dim array G[ ][ ]. This array contains information about connected vertices
for each other. To make it clear, see table below:
Vertex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
.
119
120
121
Connected vertices
2
1, 3
2, 4
3, 5
4, 6
5, 7, 14
6, 8
7, 9
8, 10
9, 11
10, 12
11, 13
12
6, 15
14, 16
15, 17
16, 18
17, 19
18, 20
19, 21
20, 22, 42
21, 23
22, 24
23, 25
24, 26
..
.
118, 120
119, 121
120
From the table, we saw that vertex 1 only connect to vertex 2 or we can say L(1) = {2}. Vertex
2 connect to vertex 1 and vertex 3 or we can say L(2) = {1, 3}, etc.
Step 4. Make all vertices as unvisited, because we havent through it yet.
Step 5. Begin solve the maze from vertex 1 (v = 1).
Step 6. First, we check whether the vertex is already visited or not. If it hasnt visited yet then
mark it as visited, but if it already visited then mark it as blocked. Visited mark means we can
visit it again once, but blocked means we cant visit it again. In the case of vertex 1, this vertex
is unvisited then mark it as visited.
Step 7. Print v as a sign that we already passed through it. In this case (v = 1), print 1.
Step 8. Check how many unvisited vertex in L (v).
If there are 2 unvisited vertices then we can choose any vertex randomly.
If there is only 1 unvisited vertex then we only can choose the only unvisited vertex.
Figure 6: We only choose unvisited vertex and ignore visited and blocked vertex.
If there is no unvisited vertex then we choose visited vertex as direction, we always ignore
blocked vertex.
Figure 7: There are no unvisited vertex, so we move back through visited vertex until we find
unvisited vertex again.
Flowchart
Results
After ran the program 3 times, it given 3 different results:
Result 1.
Path: {1 - 2 - 3 - 4 - 5 - 6 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 - 42 - 43 - 44 - 45 - 46 - 47 - 48 - 71 - 72 - 73 - 74 - 75 - 76 77 - 78 - 79 - 80 - 81 - 82 - 83 - 84 - 85 - 86 - 87 - 88 - 89 - 90 - 91 - 91 - 93 - 94 - 95 - 96 - 97 - 98 - 99 - 100 - 101 - 102 103 - 104 - 105 - 106 - 107 - 108 - 109 - 110 - 111 - 112 - 113 - 114 - 115 - 116 - 117 - 118 - 119 - 120 - 121}.
Result 2.
Path: {1 - 2 - 3 - 4 - 5 - 6 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 - 42 - 43 - 44 - 45 - 46 - 47 - 48 - 49 - 50 - 51 - 52 - 53 - 54 70 - 104 - 105 - 106 - 107 - 108 - 109 - 110 - 111 - 112 - 113 - 114 - 115 - 116 - 117 - 118 - 119 - 120 - 121}.
Result 3.
Path: {1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 12 - 11 - 10 - 9 - 8 - 7 - 6 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21
23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 - 38 - 39 - 40 - 41 - 40 - 39 - 38 - 37 - 36 - 35 - 34
32 - 31 - 30 - 29 - 28 - 27 - 26 - 25 - 24 - 23 - 22 - 21 - 42 - 43 - 44 - 45 - 46 - 47 - 48 - 49 - 50 - 51 - 52 - 53 - 54 - 55
57 - 58 - 59 - 60 - 61 - 62 - 63 - 64 - 65 - 66 - 67 - 68 - 69 - 68 - 67 - 66 - 65 - 64 - 63 - 62 - 61 - 60 - 59 - 58 - 57 - 56
54 - 70 - 104 - 105 - 106 - 107 - 108 - 109 - 110 - 111 - 112 - 113 - 114 - 115 - 116 - 117 - 118 - 119 - 120 - 121}.
22
33
56
55
If we make a comparison about these three results, result 2 give the best path because it didnt
have any wrong ways and give the shortest path. Result 1 is didnt have any wrong ways too, but
it give a longer way than result 2. In result 3, it has wrong ways three times. It was illustrated
by dark green lines. These result show us that this algorithm can still solve the problem even
though sometime it takes any wrong way. So, for the conclusion, we can say that DFS algorithm
is a useful algorithm for solving maze problem.
Source Code
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
#define
#define
#define
#define
#define
blocked
2 /* a vertex which visited two times */
visited
1 /* a vertex which visited once */
unvisited 0 /* a vertex which not visited yet */
N 121
/* N = the number of vertices */
M 3
/* M = the largest degree */
{84,86,0},
{85,87,0},
{86,88,0},
{87,89,0},
{88,90,0},
{89,91,0},
{90,92,0},
{91,93,0},
{92,94,0},
{93,95,0},
{94,96,0},
{95,97,0},
{96,98,0},
{97,99,0},
{98,100,0},
{99,101,0},
{100,102,0},
{101,103,0},
{102,104,0},
{70,105,103},
{104,106,0},
{105,107,0},
{106,108,0},
{107,109,0},
{108,110,0},
{109,111,0},
{110,112,0},
{111,113,0},
{112,114,0},
{113,115,0},
{114,116,0},
{115,117,0},
{116,118,0},
{117,119,0},
{118,120,0},
{119,121,0},
{120,0,0},
};
//85
//86
//87
//88
//89
//90
//91
//92
//93
//94
//95
//96
//97
//98
//99
//100
//101
//102
//103
//104
//105
//106
//107
//108
//109
//110
//111
//112
//113
//114
//115
//116
//117
//118
//119
//120
//121
/* to mark a vertex */
int mark[N+1];
void search(int v) {
int w, r, count = 0;
/* check that v is already visited or not */
switch (mark[v]) {
case unvisited:
mark[v] = visited;
cout << v << endl;
break;
case visited:
mark[v] = blocked;
cout << v << endl;
break;
}
/* find the number of unvisited vertex */
for (int i = 0; i < M; i++) {
w = G[v][i];
if (w != 0 && mark[w] == unvisited)
count++;
}
/*
case 2: pick a random vertex from 2 unvisited vertices
case 1: pick an unvisited vertex
case 0: pick backward path because there is no unvisited vertex
*/
switch (count) {
case 2:
do {
r = rand() % 3;
w = G[v][r];
}while(w == 0 || mark[w] == visited);
break;
case 1:
for (int i = 0; i < M; i++) {
if (G[v][i] != 0 && mark[G[v][i]] == unvisited)
w = G[v][i];
}
break;
case 0:
for (int i = 0; i < M; i++) {
if (G[v][i] != 0 && mark[G[v][i]] == visited)
w = G[v][i];
}
}
/* mark vertex 13, 41, and 69 as visited because we want to make it only visited once */
if (w == 13 || w == 41 || w == 69) {
mark[w] = visited;
}
/* if already reach the end point then we already solve the maze */
if (w == 121) {
cout << w << endl;
10
return;
}
/* recursive */
search(w);
}
int main() {
/* initialize random seed */
srand ( time(NULL) );
/* initialization */
for (int i = 1; i <= N; i++) {
mark[i] = unvisited;
}
search(1);
return 0;
}
11