defth first search algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • majidmajid
    New Member
    • Feb 2007
    • 17

    defth first search algorithm

    hi....
    i need code for defth first search alogithm.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by majidmajid
    hi....
    i need code for defth first search alogithm.
    for an algorithm have a look at
    http://en.wikipedia.or g/wiki/Depth-first_search

    Comment

    • sangeetha jagannathan
      New Member
      • Jan 2007
      • 22

      #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,ar gv);
      label=0;
      for (v=1; v<=n; v++) val[v]=0;
      for (v=1; v<=n; v++)
      if (val[v] == 0)
      DFS(v);
      }

      Comment

      • gowda gopala
        New Member
        • Jan 2007
        • 4

        #4
        Originally posted by sangeetha jagannathan
        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,ar gv);
        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

        Comment

        Working...