使用链接列表,c ++的图表的BFS

时间:2016-06-10 17:12:25

标签: c++ data-structures breadth-first-search

我尝试使用链接列表为图表实现BFS但是我遇到了一些问题,使用队列它只显示该源的节点而不是它之后的节点。答案应该是2031,但我只得到203。

代码如下:

            #include <iostream>
            #include <cmath>
            #include <vector>
            #include <stdlib.h>
            #include <queue>

            using namespace std;

            class linkListNode
            {
            public:
                linkListNode *next;
                int destination;
                bool visited;

                linkListNode()
                {
                    next = NULL;
                    destination =0;
                    visited=false;
                }
            };

            class linkList
            {
            public:
                linkListNode *head;

                linkList()
                {
                    head = NULL;
                }

                // append type insert
                void insert(int value)
                {
                    linkListNode *temp2 = new linkListNode;
                    temp2->destination = value;
                    temp2->next = NULL;
                    linkListNode *nodePtr = new linkListNode;

                    if (head == NULL)
                    {
                        head = temp2;
                        temp2->next = NULL;
                    }

                    else
                    {
                        nodePtr = head;
                        while (nodePtr->next)
                        {
                            nodePtr = nodePtr->next;
                        }
                        nodePtr->next = temp2;
                    }
                }

                void display()
                {
                    linkListNode *temp = new linkListNode;
                    temp = head;

                    while (temp)
                    {
                        cout << temp->destination << " --> ";
                        temp = temp->next;
                    }
                    cout << endl;
                }


                int size()
                {
                    linkListNode *temp = head;
                    int sizer = 0;
                    while (temp)
                    {
                        sizer++;
                        temp = temp->next;
                    }
                    return sizer;
                }
            };

            class edge
            {
            public:
                int origin;
                linkList final;
                bool visited;

                edge()
                {
                    //origin = NULL;
                    //cost=0;
                    visited=false;
                }
            };

            class  graph
            {  
            private:
                vector <edge> vectorOfEdges;
                int vertices;

            public:
                graph(int v)
                {
                    vertices = v;
                    vectorOfEdges.clear();
                }

                void addRoute(int ori, int dest)
                {
                    int counter = 0;
                    for (int i = 0; i<vectorOfEdges.size(); i++)
                    {
                        edge e = vectorOfEdges[i];
                        if (e.origin== ori)
                        {
                            counter = 1; // means element was present in the list
                            e.final.insert(dest);
                        }
                        vectorOfEdges[i] = e;
                    }

                    if (counter == 0) // when counter is set to zero, this means that the element was not found in the vector and needs to be pushed
                    {
                        edge e;
                        e.origin = ori;
                        e.final.insert(dest);
                        vectorOfEdges.push_back(e);
                    }
                }

                void printGraph()
                {
                    edge e;
                    for (int i = 0; i<vectorOfEdges.size(); i++)
                    {
                        e = vectorOfEdges[i];
                        cout << e.origin << ":- ";
                        e.final.display();
                        cout << endl;
                    }
                }

                int sizeOfEdge(edge e)
                {
                    int x = e.final.size() + 1;
                    return x;
                }

                int max(int one, int two)
                {
                    if (one > two)
                    {
                        return one;
                    }

                    else
                    {
                        return two;
                    }
                }

                void BFS(int start)
                {
                    edge e;
                    queue <int> q;
                    int save_index=0;
                    for (int i=0;i<vectorOfEdges.size();i++)
                    {
                        e=vectorOfEdges[i];
                        if (e.origin == start)
                        {
                            save_index=i;
                            q.push(e.origin);
                            e.visited=true;
                            break;
                        }
                    }

                    while (!q.empty())
                    {
                        int x=q.front();
                        cout << x << " " ;
                        q.pop();

                        linkListNode *l = e.final.head;
                        while (l)
                        {
                            if (l->visited == false)
                            {
                                q.push(l->destination);
                                l->visited=true;
                                l=l->next;
                            }

                            else
                            {
                                l=l->next;
                            }
                        }
                    }   
                }

            };

            int main()
            {
                graph g(4);
                g.addRoute(0, 1);
                g.addRoute(0, 2);
                g.addRoute(1, 2);
                g.addRoute(2, 0);
                g.addRoute(2, 3);
             // g.printGraph();
                //cout << "Following is Breadth First Traversal (starting from vertex 2) \n";
                g.BFS(1);
            }

0 个答案:

没有答案