0% found this document useful (0 votes)
21 views

Programing Report DFS

The document describes using depth-first search (DFS) to solve a maze problem. It provides details on the DFS algorithm steps, including representing the maze as a graph and using arrays to track visited vertices. Sample output showing different paths produced by running the algorithm multiple times is also included, along with the source code implementing the DFS approach.

Uploaded by

raden_faqih
Copyright
© Attribution Non-Commercial (BY-NC)
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)
21 views

Programing Report DFS

The document describes using depth-first search (DFS) to solve a maze problem. It provides details on the DFS algorithm steps, including representing the maze as a graph and using arrays to track visited vertices. Sample output showing different paths produced by running the algorithm multiple times is also included, along with the source code implementing the DFS approach.

Uploaded by

raden_faqih
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 11

COMBINATORICS

Programming Report
Ahnaf Faqih Shaimy (1113011466)
Kanazawa University
June 22, 2012

Given a maze:

Figure 1: Maze problem

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.

Figure 2: Numbered maze.

Figure 3: A tree from the maze.

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.

Figure 4: We prefer to choose unvisited vertex randomly, regardless visited vertex.

If there is only 1 unvisited vertex then we only can choose the only unvisited vertex.

Figure 5: We only choose unvisited vertex and ignore visited 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.

Information of colored lines:

Finally, mark the chosen vertex as w.


In the case v = 1, it will choose vertex 2 because it is the only unvisited vertex. So, mark w = 2.
Step 9. Now we enter a special case. If we choose a corner vertex, like vertex 13, 41, or 69 (see
Figure 2), then we directly make this vertex as visited because we want to make this vertex can
only visited once. In other words, we dont want to trapped on the corner.
Step 10. Back to step 6 with using the chosen vertex (v = w) until reach the end point.

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 */

/* G[vertex number][i] = one of adjacent vertex unless zero */


int G[N+1][M] = {
{0,0,0},
//0
{2,0,0},
//1
{1,3,0},
//2
{2,4,0},
//3
{3,5,0},
//4
{4,6,0},
//5
{5,14,7},
//6
{6,8,0},
//7
{7,9,0},
//8
{8,10,0},
//9
{9,11,0},
//10
{10,12,0},
//11
{11,13,0},
//12
{12,0,0},
//13
{6,15,0},
//14
{14,16,0},
//15
{15,17,0},
//16
{16,18,0},
//17
{17,19,0},
//18
{18,20,0},
//19
{19,21,0},
//20
{20,42,22},
//21
{21,23,0},
//22
{22,24,0},
//23
{23,25,0},
//24
{24,26,0},
//25
{25,27,0},
//26
{26,28,0},
//27
{27,29,0},
//28
{28,30,0},
//29
{29,31,0},
//30
{30,32,0},
//31
{31,33,0},
//32
{32,34,0},
//33
{33,35,0},
//34
{34,36,0},
//35
{35,37,0},
//36
{36,38,0},
//37
{37,39,0},
//38
{38,40,0},
//39
{39,41,0},
//40
{40,0,0},
//41
{21,43,0},
//42
{42,44,0},
//43
{43,45,0},
//44
{44,46,0},
//45
{45,47,0},
//46
{46,48,0},
//47
{47,49,71},
//48
{48,50,0},
//49
{49,51,0},
//50
{50,52,0},
//51
{51,53,0},
//52
{52,54,0},
//53
{53,70,55},
//54
{54,56,0},
//55
{55,57,0},
//56
{56,58,0},
//57
{57,59,0},
//58
{58,60,0},
//59
{59,61,0},
//60
{60,62,0},
//61
{61,63,0},
//62
{62,64,0},
//63
{63,65,0},
//64
{64,66,0},
//65
{65,67,0},
//66
{66,68,0},
//67
{67,69,0},
//68
{68,0,0},
//69
{54,104,0},
//70
{48,72,0},
//71
{71,73,0},
//72
{72,74,0},
//73
{73,75,0},
//74
{74,76,0},
//75
{75,77,0},
//76
{76,78,0},
//77
{77,79,0},
//78
{78,80,0},
//79
{79,81,0},
//80
{80,82,0},
//81
{81,83,0},
//82
{82,84,0},
//83
{83,85,0},
//84

{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

You might also like