dfs depth first
(1) Access an adjacent inaccessible vertex, mark it, and put it on the stack.
(2) When rule 1 cannot be executed, if the stack is not empty, a vertex pops up from the stack.
(3) If rules 1 and 2 cannot be executed, the entire search process is completed.
bfs breadth first
1. Put the root node in the queue first.
2. Remove the first node from the queue and verify that it is the target.
If the target is found, the search ends and the result is returned.
Otherwise, all its direct child nodes that have not been verified will be added to the queue.
3. If the queue is empty, the whole graph has been checked - that is, there is no target to search in the graph. End the search and return target not found.
4. Repeat step 2.
class Program
{
public class MyPoint
{
public MyPoint parent { get; set; }
public int X { get; set; }
public int Y { get; set; }
public MyPoint(int a, int b)
{
this.X = a;
this.Y = b;
}
}
static void WritePath(MyPoint p)
{
if (p.parent != null)
{
WritePath(p.parent);
Console.Write("(" + p.X + "," + p.Y + ")-->");
}
}
static void Main(string[] args)
{
int[,] data = new int[,]{ //Initialize data 1 as road 0 as wall
{1,1,0,1,1},
{1,0,1,1,1},
{1,0,1,0,0},
{1,0,1,1,1},
{1,1,1,0,1},
{1,1,1,1,1}
};
MyPoint p_bfs = new MyPoint(0, 0);
p_bfs.parent = null;
BFS(p_bfs, data);
data = new int[,]{ //Initialize data 1 as road 0 as wall
{1,1,0,1,1},
{1,0,1,1,1},
{1,0,1,0,0},
{1,0,1,1,1},
{1,1,1,0,1},
{1,1,1,1,1}
};
MyPoint p_dfs = new MyPoint(0, 0);
p_dfs.parent = null;
DFS(p_dfs, data);
Console.Read();
}
static void DFS(MyPoint qp,int[,] data)
{
for (int i = -1; i < 2; i++) //Traversal of reachable nodes
{
for (int j = -1; j < 2; j++)
{
if ((qp.X + i >= 0) && (qp.X + i <= 5) && (qp.Y + j >= 0) && (qp.Y + j <= 4) && (qp.X + i == qp.X || qp.Y == qp.Y + j)) //Whether to traverse only up and down left and right
{
if (data[qp.X + i, qp.Y + j] == 1)
{
if (qp.X + i == 5 && qp.Y + j == 4) //End point or not
{
Console.WriteLine("");
Console.Write("DFS:");
WritePath(qp); //Recursive output path
Console.Write("(5,4)");
}
else
{
MyPoint temp = new MyPoint(qp.X + i, qp.Y + j); //Join queue
temp.parent = qp;
data[qp.X + i, qp.Y + j] = -1;
DFS(temp,data);
}
}
}
}
}
}
static void BFS(MyPoint p, int[,] data) {
Queue q = new Queue();
data[0, 0] = -1;
q.Enqueue(p);
while (q.Count > 0)
{
MyPoint qp = (MyPoint)q.Dequeue();
for (int i = -1; i < 2; i++) //Traversal of reachable nodes
{
for (int j = -1; j < 2; j++)
{
if ((qp.X + i >= 0) && (qp.X + i <= 5) && (qp.Y + j >= 0) && (qp.Y + j <= 4) && (qp.X + i == qp.X || qp.Y == qp.Y + j)) //Whether to traverse only up and down left and right
{
if (data[qp.X + i, qp.Y + j] == 1)
{
if (qp.X + i == 5 && qp.Y + j == 4) //End point or not
{
Console.Write("BFS:(0,0)-->");
WritePath(qp); //Recursive output path
Console.Write("(5,4)");
Console.WriteLine("");
}
else
{
MyPoint temp = new MyPoint(qp.X + i, qp.Y + j); //Join queue
data[qp.X + i, qp.Y + j] = -1;
temp.parent = qp;
q.Enqueue(temp);
}
}
}
}
}
}
}
static void Dijkstra(MyPoint p, int[,] data)
{
//dijkstra is equivalent to BFS when the weights are the same
}
}