Depth First Search using Adjacency List

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












My aim to implement adjacency list correctly. After takin help from here, I have implemented it. I have tried to implement it here, but it was suggested that it is most likely adjacency matrix. Help me to improve my code. Should I add any other function for DFS.



#include <iostream>
#include <vector>

class Graph

int vertex_count;
enum Color WHITE, GRAY, BLACK;

struct Vertex

int id;
Color color;
Vertex(int _id) : id(_id),
color(Color::WHITE)

;

struct Adj_list_node

int dest_id;
Adj_list_node * next;
Adj_list_node(int _dest_id) : dest_id(_dest_id),
next (nullptr)

;

struct Adj_list

Adj_list_node *head;
;
std::vector<Vertex> vertices;
std::vector<Adj_list> adjacent;
public:
Graph(int);
void add_edge(int, int);
void depth_first_search();

private:
void depth_first_search_visit(int);
;

Graph::Graph(int v)

vertex_count = v;
adjacent.resize(vertex_count);

for (int i = 0; i < vertex_count; i++)

vertices.push_back( Vertex(i) );
adjacent[i].head = nullptr;



void Graph::depth_first_search()

for (const auto& u: vertices)

if (vertices[u.id].color == WHITE)

depth_first_search_visit(u.id);




void Graph::depth_first_search_visit(int u)

vertices[u].color = GRAY;
std::cout << vertices[u].id <<" ";
const auto& v = adjacent[u].head;
if (vertices[v->dest_id].color == WHITE)

depth_first_search_visit(v->dest_id);

vertices[u].color = BLACK;


void Graph::add_edge(int src, int dest)

Adj_list_node *node = new Adj_list_node(dest);
if (adjacent[src].head == nullptr)

adjacent[src].head = node;

else

Adj_list_node *tmp = adjacent[src].head;
while (tmp->next != nullptr)
tmp = tmp->next;

tmp->next = node;


//undirected graph
node = new Adj_list_node(src);
if (adjacent[dest].head == nullptr)

adjacent[dest].head = node;

else

Adj_list_node *tmp = adjacent[dest].head;
while (tmp->next != nullptr)
tmp = tmp->next;

tmp->next = node;



int main()

Graph grp(4);
grp.add_edge(0, 1);
grp.add_edge(1, 2);
grp.add_edge(2, 3);
grp.add_edge(2, 1);
grp.add_edge(0, 3);
grp.depth_first_search();
std::cout << "n";







share|improve this question

























    up vote
    1
    down vote

    favorite












    My aim to implement adjacency list correctly. After takin help from here, I have implemented it. I have tried to implement it here, but it was suggested that it is most likely adjacency matrix. Help me to improve my code. Should I add any other function for DFS.



    #include <iostream>
    #include <vector>

    class Graph

    int vertex_count;
    enum Color WHITE, GRAY, BLACK;

    struct Vertex

    int id;
    Color color;
    Vertex(int _id) : id(_id),
    color(Color::WHITE)

    ;

    struct Adj_list_node

    int dest_id;
    Adj_list_node * next;
    Adj_list_node(int _dest_id) : dest_id(_dest_id),
    next (nullptr)

    ;

    struct Adj_list

    Adj_list_node *head;
    ;
    std::vector<Vertex> vertices;
    std::vector<Adj_list> adjacent;
    public:
    Graph(int);
    void add_edge(int, int);
    void depth_first_search();

    private:
    void depth_first_search_visit(int);
    ;

    Graph::Graph(int v)

    vertex_count = v;
    adjacent.resize(vertex_count);

    for (int i = 0; i < vertex_count; i++)

    vertices.push_back( Vertex(i) );
    adjacent[i].head = nullptr;



    void Graph::depth_first_search()

    for (const auto& u: vertices)

    if (vertices[u.id].color == WHITE)

    depth_first_search_visit(u.id);




    void Graph::depth_first_search_visit(int u)

    vertices[u].color = GRAY;
    std::cout << vertices[u].id <<" ";
    const auto& v = adjacent[u].head;
    if (vertices[v->dest_id].color == WHITE)

    depth_first_search_visit(v->dest_id);

    vertices[u].color = BLACK;


    void Graph::add_edge(int src, int dest)

    Adj_list_node *node = new Adj_list_node(dest);
    if (adjacent[src].head == nullptr)

    adjacent[src].head = node;

    else

    Adj_list_node *tmp = adjacent[src].head;
    while (tmp->next != nullptr)
    tmp = tmp->next;

    tmp->next = node;


    //undirected graph
    node = new Adj_list_node(src);
    if (adjacent[dest].head == nullptr)

    adjacent[dest].head = node;

    else

    Adj_list_node *tmp = adjacent[dest].head;
    while (tmp->next != nullptr)
    tmp = tmp->next;

    tmp->next = node;



    int main()

    Graph grp(4);
    grp.add_edge(0, 1);
    grp.add_edge(1, 2);
    grp.add_edge(2, 3);
    grp.add_edge(2, 1);
    grp.add_edge(0, 3);
    grp.depth_first_search();
    std::cout << "n";







    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My aim to implement adjacency list correctly. After takin help from here, I have implemented it. I have tried to implement it here, but it was suggested that it is most likely adjacency matrix. Help me to improve my code. Should I add any other function for DFS.



      #include <iostream>
      #include <vector>

      class Graph

      int vertex_count;
      enum Color WHITE, GRAY, BLACK;

      struct Vertex

      int id;
      Color color;
      Vertex(int _id) : id(_id),
      color(Color::WHITE)

      ;

      struct Adj_list_node

      int dest_id;
      Adj_list_node * next;
      Adj_list_node(int _dest_id) : dest_id(_dest_id),
      next (nullptr)

      ;

      struct Adj_list

      Adj_list_node *head;
      ;
      std::vector<Vertex> vertices;
      std::vector<Adj_list> adjacent;
      public:
      Graph(int);
      void add_edge(int, int);
      void depth_first_search();

      private:
      void depth_first_search_visit(int);
      ;

      Graph::Graph(int v)

      vertex_count = v;
      adjacent.resize(vertex_count);

      for (int i = 0; i < vertex_count; i++)

      vertices.push_back( Vertex(i) );
      adjacent[i].head = nullptr;



      void Graph::depth_first_search()

      for (const auto& u: vertices)

      if (vertices[u.id].color == WHITE)

      depth_first_search_visit(u.id);




      void Graph::depth_first_search_visit(int u)

      vertices[u].color = GRAY;
      std::cout << vertices[u].id <<" ";
      const auto& v = adjacent[u].head;
      if (vertices[v->dest_id].color == WHITE)

      depth_first_search_visit(v->dest_id);

      vertices[u].color = BLACK;


      void Graph::add_edge(int src, int dest)

      Adj_list_node *node = new Adj_list_node(dest);
      if (adjacent[src].head == nullptr)

      adjacent[src].head = node;

      else

      Adj_list_node *tmp = adjacent[src].head;
      while (tmp->next != nullptr)
      tmp = tmp->next;

      tmp->next = node;


      //undirected graph
      node = new Adj_list_node(src);
      if (adjacent[dest].head == nullptr)

      adjacent[dest].head = node;

      else

      Adj_list_node *tmp = adjacent[dest].head;
      while (tmp->next != nullptr)
      tmp = tmp->next;

      tmp->next = node;



      int main()

      Graph grp(4);
      grp.add_edge(0, 1);
      grp.add_edge(1, 2);
      grp.add_edge(2, 3);
      grp.add_edge(2, 1);
      grp.add_edge(0, 3);
      grp.depth_first_search();
      std::cout << "n";







      share|improve this question











      My aim to implement adjacency list correctly. After takin help from here, I have implemented it. I have tried to implement it here, but it was suggested that it is most likely adjacency matrix. Help me to improve my code. Should I add any other function for DFS.



      #include <iostream>
      #include <vector>

      class Graph

      int vertex_count;
      enum Color WHITE, GRAY, BLACK;

      struct Vertex

      int id;
      Color color;
      Vertex(int _id) : id(_id),
      color(Color::WHITE)

      ;

      struct Adj_list_node

      int dest_id;
      Adj_list_node * next;
      Adj_list_node(int _dest_id) : dest_id(_dest_id),
      next (nullptr)

      ;

      struct Adj_list

      Adj_list_node *head;
      ;
      std::vector<Vertex> vertices;
      std::vector<Adj_list> adjacent;
      public:
      Graph(int);
      void add_edge(int, int);
      void depth_first_search();

      private:
      void depth_first_search_visit(int);
      ;

      Graph::Graph(int v)

      vertex_count = v;
      adjacent.resize(vertex_count);

      for (int i = 0; i < vertex_count; i++)

      vertices.push_back( Vertex(i) );
      adjacent[i].head = nullptr;



      void Graph::depth_first_search()

      for (const auto& u: vertices)

      if (vertices[u.id].color == WHITE)

      depth_first_search_visit(u.id);




      void Graph::depth_first_search_visit(int u)

      vertices[u].color = GRAY;
      std::cout << vertices[u].id <<" ";
      const auto& v = adjacent[u].head;
      if (vertices[v->dest_id].color == WHITE)

      depth_first_search_visit(v->dest_id);

      vertices[u].color = BLACK;


      void Graph::add_edge(int src, int dest)

      Adj_list_node *node = new Adj_list_node(dest);
      if (adjacent[src].head == nullptr)

      adjacent[src].head = node;

      else

      Adj_list_node *tmp = adjacent[src].head;
      while (tmp->next != nullptr)
      tmp = tmp->next;

      tmp->next = node;


      //undirected graph
      node = new Adj_list_node(src);
      if (adjacent[dest].head == nullptr)

      adjacent[dest].head = node;

      else

      Adj_list_node *tmp = adjacent[dest].head;
      while (tmp->next != nullptr)
      tmp = tmp->next;

      tmp->next = node;



      int main()

      Graph grp(4);
      grp.add_edge(0, 1);
      grp.add_edge(1, 2);
      grp.add_edge(2, 3);
      grp.add_edge(2, 1);
      grp.add_edge(0, 3);
      grp.depth_first_search();
      std::cout << "n";









      share|improve this question










      share|improve this question




      share|improve this question









      asked Feb 26 at 14:53









      coder

      911425




      911425

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188377%2fdepth-first-search-using-adjacency-list%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188377%2fdepth-first-search-using-adjacency-list%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation