thanhthanh
11-28-2007, 06:31 PM
I got these source code from Wikipedia
http://en.wikipedia.org/wiki/Breadth-first_search
They are about Bread first search of graph.Any one know about that?
I quote it here
Algorithm (informal)
1. Put the ending node (the root node) in the queue.
2. Pull a node from the beginning of the queue and examine it.
* If the searched element is found in this node, quit the search and return a result.
* Otherwise push all the (so-far-unexamined) successors (the direct child nodes) of this node into the end of the queue, if there are any.
3. If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".
4. Repeat from Step 2.
Note: Using a stack instead of a queue to store the nodes yet to be visited would turn this algorithm into a Depth-first search.
[edit] C implementation
Algorithm of Breadth-first search:
void BFS(VLink G[], int v) {
int w;
VISIT(v); /*visit vertex v*/
visited[v] = 1; /*mark v as visited : 1 */
ADDQ(Q,v);
while(!QMPTYQ(Q)) {
v = DELQ(Q); /*Dequeue v*/
w = FIRSTADJ(G,v); /*Find first neighbor, return -1 if no neighbor*/
while(w != -1) {
if(visited[w] == 0) {
VISIT(w); /*visit vertex v*/
ADDQ(Q,w); /*Enqueue current visited vertext w*/
visited[w] = 1; /*mark w as visited*/
}
W = NEXTADJ(G,v); /*Find next neighbor, return -1 if no neighbor*/
}
}
}
Main Algorithm of apply Breadth-first search to graph G=(V,E):
void TRAVEL_BFS(VLink G[], int visited[], int n) {
int i;
for(i = 0; i < n; i ++) {
visited[i] = 0; /* Mark initial value as 0 */
}
for(i = 0; i < n; i ++)
if(visited[i] == 0)
BFS(G,i);
}
I think that inforder to find out the neighbour of a vertex of a graph, the second parameter must be VLink like G,but ,here,it just an int.i don't know how to find the neighbour of a node if we just give the value.( int v)
void BFS(VLink G[], int v)
w = FIRSTADJ(G,v);
W = NEXTADJ(G,v);
http://en.wikipedia.org/wiki/Breadth-first_search
They are about Bread first search of graph.Any one know about that?
I quote it here
Algorithm (informal)
1. Put the ending node (the root node) in the queue.
2. Pull a node from the beginning of the queue and examine it.
* If the searched element is found in this node, quit the search and return a result.
* Otherwise push all the (so-far-unexamined) successors (the direct child nodes) of this node into the end of the queue, if there are any.
3. If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".
4. Repeat from Step 2.
Note: Using a stack instead of a queue to store the nodes yet to be visited would turn this algorithm into a Depth-first search.
[edit] C implementation
Algorithm of Breadth-first search:
void BFS(VLink G[], int v) {
int w;
VISIT(v); /*visit vertex v*/
visited[v] = 1; /*mark v as visited : 1 */
ADDQ(Q,v);
while(!QMPTYQ(Q)) {
v = DELQ(Q); /*Dequeue v*/
w = FIRSTADJ(G,v); /*Find first neighbor, return -1 if no neighbor*/
while(w != -1) {
if(visited[w] == 0) {
VISIT(w); /*visit vertex v*/
ADDQ(Q,w); /*Enqueue current visited vertext w*/
visited[w] = 1; /*mark w as visited*/
}
W = NEXTADJ(G,v); /*Find next neighbor, return -1 if no neighbor*/
}
}
}
Main Algorithm of apply Breadth-first search to graph G=(V,E):
void TRAVEL_BFS(VLink G[], int visited[], int n) {
int i;
for(i = 0; i < n; i ++) {
visited[i] = 0; /* Mark initial value as 0 */
}
for(i = 0; i < n; i ++)
if(visited[i] == 0)
BFS(G,i);
}
I think that inforder to find out the neighbour of a vertex of a graph, the second parameter must be VLink like G,but ,here,it just an int.i don't know how to find the neighbour of a node if we just give the value.( int v)
void BFS(VLink G[], int v)
w = FIRSTADJ(G,v);
W = NEXTADJ(G,v);