473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

defth first search algorithm

hi....
i need code for defth first search alogithm.
Mar 12 '07 #1
3 2111
horace1
1,510 Expert 1GB
hi....
i need code for defth first search alogithm.
for an algorithm have a look at
http://en.wikipedia.org/wiki/Depth-first_search
Mar 12 '07 #2
hi
u need code in which programming language?
i am sending you the pseudocode, you can change this to any programming language easily just by applying the syntax of that particular language.

dfs(v)
process(v)
mark v as visited
for all vertices i adjacent to v not visited
dfs(i)

another version

dfs(graph G)
{
list L = empty
tree T = empty
choose a starting vertex x
search(x)
while(L is not empty)
remove edge (v, w) from end of L
if w not yet visited
{
add (v, w) to T
search(w)
}
}

search(vertex v)
{
visit v
for each edge (v, w)
add edge (v, w) to end of L
}


there is a small version of it in C programming language

take a look at it

typedef int NODE;
int A[MAX][MAX], B[MAX][MAX];
int n, label, val[MAX];

void DFS(NODE v){
NODE w;
val[v] = ++label;
for (w=1; w<=n; w++){
if (A[v][w] != 0)
if (val[w] == 0) {
B[v][w] = 1;
DFS(w);
}
}
}

main(int argc, char *argv[]) {
NODE v;
getdata(argc,argv);
label=0;
for (v=1; v<=n; v++) val[v]=0;
for (v=1; v<=n; v++)
if (val[v] == 0)
DFS(v);
}
Mar 12 '07 #3
hi
u need code in which programming language?
i am sending you the pseudocode, you can change this to any programming language easily just by applying the syntax of that particular language.

dfs(v)
process(v)
mark v as visited
for all vertices i adjacent to v not visited
dfs(i)

another version

dfs(graph G)
{
list L = empty
tree T = empty
choose a starting vertex x
search(x)
while(L is not empty)
remove edge (v, w) from end of L
if w not yet visited
{
add (v, w) to T
search(w)
}
}

search(vertex v)
{
visit v
for each edge (v, w)
add edge (v, w) to end of L
}


there is a small version of it in C programming language

take a look at it

typedef int NODE;
int A[MAX][MAX], B[MAX][MAX];
int n, label, val[MAX];

void DFS(NODE v){
NODE w;
val[v] = ++label;
for (w=1; w<=n; w++){
if (A[v][w] != 0)
if (val[w] == 0) {
B[v][w] = 1;
DFS(w);
}
}
}

main(int argc, char *argv[]) {
NODE v;
getdata(argc,argv);
label=0;
for (v=1; v<=n; v++) val[v]=0;
for (v=1; v<=n; v++)
if (val[v] == 0)
DFS(v);
}
nice psuedo code
Mar 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
3
by: alaa123 | last post by:
hi plz do not ignore me I really neeed ur help I am working on a progarmme to implement First Search and Breadth Search I did the first search algorithm but I need help with implementing the...
9
by: Rick | last post by:
I have a large list of objects where each object has a unique (non-overlapping) date range. The list is sorted chronologically. What is the most efficient way to search this list for a single...
4
by: sklett | last post by:
I realize this could be a little off-topic, but there are some great minds on this NG and I hope you can let me slide this time ;0) I'm designing our system to manage what products can fit in...
4
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and...
0
by: chrisotreh | last post by:
hi everyone, i need a simple code of IDA* algorithm. this algorithm is a method of heuristic search.this algorithm is the result of enhancement of Depth First Search combined with A* algorithm.. ...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
2
by: shashankbs | last post by:
Given a topology and a certain node X, find the shortest path tree with X as the root. * Input: a topology file similar to the following (which is a three-node ring) ...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.