使用具有两个链表

时间:2016-12-09 20:21:10

标签: c++ algorithm graph

我是大学生,这是我第一次在这里提问。首先感谢您花时间阅读。我们有一个关于Graphs的项目。到目前为止,我通过矩阵和邻接列表创建了图形。我的邻接列表版本由2个链表组成(只是为了允许添加我想要的顶点)。 现在我正在尝试进行深度优先搜索。我做了一些研究而且认真对待我并不太了解,所以我按照自己的想法。它正在工作,但我不确定它是否正确或复杂性是否为O(V + E)v指的是顶点和E边缘..任何人都可以阅读它并告诉我它是否正确?如果是复杂度O(V + E)怎么样? PS:请只使用c ++,因为它是我所知道的唯一语言。 非常感谢你的帮助

我在DFS中所做的是

1-do while循环遍历所有顶点并在每个顶点上调用DFS 在每个顶点上,调用DFS,顶点的颜色为灰色('g') 3-another while循环遍历目标顶点的子节点(边缘) 4对每个孩子,另一个while循环将通过search->搜索相应的边缘。 value == child-> value
5 - 当它找到顶点时,它将再次调用DFS(所以重复步骤2到5) 6-完成后,颜色顶点黑色一个接一个

以下是代码:

EdgeNode.h

class EdgeNode
{
public:
    int value;
    EdgeNode *Next;
};

VertexNode.h

#include "EdgeNode.h"
class VertexNode
{
public:
    int value;
    int parentIndex;
    char color;
    EdgeNode *Child;
    VertexNode *Next;
};

Graph.h

#include "VertexNode.h"

class Graph{
public:

    int NbVertex;
    int time;
    VertexNode *s;
    Graph();
    void AddVertex(int value);
    bool AddEdge(int parentIndex, int value);
    void PrintVertex();
    void PrintGraph();
    void DepthFirstSearch();
    void DFS(VertexNode *V);


};

Graph.cpp

#include <iostream>
#include "Graph.h"
#include <string>
using namespace std;


Graph::Graph()
{
    s = NULL;
    NbVertex = 0;
}

void Graph::AddVertex(int value)
{
    if (NbVertex == 0)
    {
        s = new VertexNode;
        s->value = value;
        s->parentIndex = NbVertex+1;
        s->color = 'w';
        s->Child = NULL;
        s->Next = NULL;
    }

    else
    {
        VertexNode *z = s;
        while (z->Next != NULL)
        {
            z = z->Next;
        }
        z->Next = new VertexNode;
        z->Next->parentIndex = NbVertex+1;
        z->Next->color = 'w';
        z->Next->value = value;
        z->Next->Child = NULL;
        z->Next->Next = NULL;
    }


    NbVertex++;
}


bool Graph::AddEdge(int parentIndex, int value)
{
    if (NbVertex == 0)
        cout << "Graph is empty" << endl;

    VertexNode *z = s;

        while (z != NULL)
        {
            if (z->parentIndex == parentIndex)
            {


                if (z->Child == NULL)
                {
                    EdgeNode *y = new EdgeNode;
                    y->value = value;
                    y->Next = NULL;
                    z->Child = y;

                }

                else
                {
                    EdgeNode *y = z->Child;
                    while (y->Next != NULL)
                    {
                        if (y->value == value)
                        {
                            cout << "The edge already exists"<<endl;
                            return false;
                        }

                        y = y->Next;
                    }
                    y->Next = new EdgeNode;
                    y->Next->value = value;
                    y->Next->Next = NULL;

                }

                return true;

            }

            z = z->Next;

        }

        cout << "The index was not found" << endl;
    return false;

}


void Graph :: PrintVertex()
{
    VertexNode *z = s;
    int count = 1;
    while (z != NULL)
    {
        cout << "vertex " << count << " : " << endl;
        cout << "Value : " << z->value << endl;
        cout << "Index : " << z->parentIndex << endl;
        cout << "Next : " << z->Next << endl;
        cout << "Child : " << z->Child << endl;
        cout << "Color : "  << z->color << endl;
        cout << endl;
        count++;
        z = z->Next;
    }

    cout << "time is : " << time;
}


void Graph::PrintGraph()
{
    VertexNode *z = s;
    int countVertex = 1;
    while (z != NULL)
    {
        cout << "vertex " << countVertex << " : " << endl;
        cout << "Value : " << z->value << endl;
        cout << "Index : " << z->parentIndex << endl;
        cout << "Next : " << z->Next << endl;
        cout << "Child : " << z->Child << endl;
        cout << endl;
        countVertex++;
        if (z->Child != NULL)
        {
            int countChild=1;
            EdgeNode *y = z->Child;
            while (y != NULL)
            {
                cout << "Child " << countChild << " : " << endl;
                cout << "Value : " << y->value << endl;
                cout <<"Next : " << y->Next << endl;
                cout << "-------------------"<<endl;
                countChild++;
                y = y->Next;
            }

        }

        cout << "*************************************"<<endl;

        z = z->Next;
    }
}



void Graph :: DepthFirstSearch()
{
    time = 0;
    VertexNode *z = s;
    while (z != NULL)
    {
        if (z->color == 'w')
        {
            DFS(z);
        }
        z = z->Next;
    }

}
void Graph::DFS(VertexNode *V)
{
    cout << "(" << V->value;
    V->color = 'g';
    time++;

    EdgeNode *Y = V->Child;
    while (Y != NULL)
    {
        VertexNode *Search = s;
        while (Search != NULL)
        {
            if (Search->value == Y->value)
            {
                if (Search->color == 'w')
                {
                    DFS(Search);
                }
                /*else
                {
                    Search->color = 'b';
                }*/
            }

            Search = Search->Next;

        }

        Y = Y->Next;
    }

    V->color = 'b';
    time++;
    cout << V->value << ")";
}

这是DFS

void Graph :: DepthFirstSearch()
{
    time = 0;
    VertexNode *z = s;
    while (z != NULL)
    {
        if (z->color == 'w')
        {
            DFS(z);
        }
        z = z->Next;
    }

}
void Graph::DFS(VertexNode *V)
{
    cout << "(" << V->value;
    V->color = 'g';
    time++;

    EdgeNode *Y = V->Child;
    while (Y != NULL)
    {
        VertexNode *Search = s;
        while (Search != NULL)
        {
            if (Search->value == Y->value)
            {
                if (Search->color == 'w')
                {
                    DFS(Search);
                }
                /*else
                {
                    Search->color = 'b';
                }*/
            }

            Search = Search->Next;

        }

        Y = Y->Next;
    }

    V->color = 'b';
    time++;
    cout << V->value << ")";
}

1 个答案:

答案 0 :(得分:0)

我不知道您的项目应该如何有条理,但使用邻接列表构建的图表的代码大约需要10行。

快速查看它,你的DFS似乎是正确的。从理论上讲,DFS(BFS)时间是O(V + E),因为它将通过所有顶点和所有边缘(总计O(| V | + | E |)。

如果您关心代码阅读和组织,请继续,但如果您更喜欢代码,我相信您可以找到更小的图形实现。

DFS的5行实现工作正常。

void DFS(graph G, int v){
    mark[v] = true;
    cout<<"Visiting "<<v<<endl;
    for(auto u : G[v]) //this means "for all u in the adjacence list of v"
        if(!mark[u]) DFS(G, u);
}