There are two common ways to search for graphs: depth-first search and breadth-first search. They will eventually reach all connected vertices. Depth-first search is implemented by stacks, and breadth-first search is achieved through queues.
Depth First search:
The figures in the following figure show the order in which the depth-first search vertices are accessed.
To achieve depth-first search, first select a starting vertex and follow three rules:
(1) If possible, access an adjacent unreachable vertex, mark it, and put it on the stack.
(2) When the Rule 1 o'clock cannot be executed, if the stack is not empty, a vertex is popped from the stack.
(3) If you cannot execute Rule 1 and Rule 2, complete the entire search process.
Breadth First Search:
In depth-first search, the algorithm behaves as if it were to stay away from the starting point as quickly as possible. Conversely, in breadth-first search, the algorithm seems to be as close to the starting point as possible. It first accesses all the adjacent points of the starting vertex and then accesses the farther area. It is implemented by queues.
The figures in the following figure show the order in which breadth-first search vertices are accessed.
To achieve breadth-first search, there are three rules to follow:
(1) Access to the next adjacent point of the next visit, the vertex must be the adjacent point of the current vertex, mark it, and insert it into the queue.
(2) If the Rule 1 o'clock cannot be performed because no vertex has not been accessed, a vertex is fetched from the queue header and made the current vertex.
(3) If you cannot execute rule 2 because the queue is empty, the search ends.
The following is a Java code for a graph class, DFS () is a depth-first search algorithm, BFS () is a breadth-first search algorithm:
Stack class for depth-first search
Class Stackx ... {
private final int size=20;
Private int[] St;
private int top;
Public Stackx () ... {
St=new Int[size];
Top=-1;
}
public void push (int j) ... {
St[++top]=j;
}
public int pop () ... {
return st[top--];
}
public int peek () ... {
return st[top];
}
public boolean IsEmpty () ... {
return top==-1;
}
}
Queue classes to implement breadth-first search
Class Queue ... {
private final int size=20;
Private int[] Quearray;
private int front;
private int rear;
Public Queue () ... {
Quearray=new Int[size];
front=0;
Rear=-1;
}
public void Insert (int j) ... {
if (rear==size-1)
Rear=-1;
Quearray[++rear]=j;
}
public int Remove () ... {
int temp=quearray[front++];
if (front==size)
front=0;
return temp;
}
public boolean IsEmpty () ... {
Return ((Rear+1==front) | | (Front+size-1==rear));
}
}
Vertex class
Class Vertex ... {
public Char label;
public Boolean wasvisited;
Public Vertex (Char Lab) ... {
Label=lab;
Wasvisited=false;
}
}
Graph class
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service