图(相邻列表)-DFS

时间:2019-12-08 11:21:34

标签: c++ depth-first-search

#include<iostream>
#include<string>
#include<vector>
using namespace std;

class DoublyEdgeLinkedList;

class vertex {                                  
public:
    DoublyEdgeLinkedList* incidentEdgeList;     
    int degree = 0;     
    int data;           
    bool visited;      
    vertex* prev;      
    vertex* next;       
    vertex(int data);
    void increase_degree() {    
        this->degree++;
    }
    void decrease_degree() {    
        this->degree--;
    }
};

class edge {
public:
    edge* prev;         
    edge* next;         
    edge* myself_InFisrt_incidentEdge;  
    edge* myself_InSecond_incidentEdge; 
    edge* myself_InTotal_EdgeList;      
    vertex* source;                     
    vertex* destination;
    bool discovery;                     
    bool back;                          
    edge(vertex* a, vertex* b) {
        this->prev = NULL;
        this->next = NULL;
        this->source = a;
        this->destination = b;
        this->myself_InFisrt_incidentEdge = NULL;
        this->myself_InSecond_incidentEdge = NULL;
        this->myself_InTotal_EdgeList = NULL;
        this->discovery = false;
        this->back = false;
    }
};

class DoublyEdgeLinkedList {        
public:
    edge* head;
    edge* tail;
    int size;
    DoublyEdgeLinkedList() {
        this->head = NULL;
        this->tail = NULL;
        this->size = 0;
    }
    void insert(edge* insertEdge) {
        if (this->head == NULL) {
            head = insertEdge;
            tail = insertEdge;
        }
        else {
            tail->next = insertEdge;
            insertEdge->prev = tail;
            tail = insertEdge;
        }
        this->size++;
    }
};

vertex::vertex(int data) {
    this->degree = 0;
    this->data = data;
    this->visited = false;
    this->prev = NULL;
    this->next = NULL;
    this->incidentEdgeList = new DoublyEdgeLinkedList();
}

class DoublyVertexLinkedList {      
public:
    vertex* head;
    vertex* tail;
    int size;
    DoublyVertexLinkedList() {
        this->head = NULL;
        this->tail = NULL;
        this->size = 0;
    }
    void insert(vertex* insertVertex) {
        if (this->head == NULL) {
            head = insertVertex;
            tail = insertVertex;
        }
        else {
            tail->next = insertVertex;
            insertVertex->prev = tail;
            tail = insertVertex;
        }
        this->size++;
    }
};

class graph {
public:
    DoublyVertexLinkedList* TotalvertexList;            
    DoublyEdgeLinkedList* TotaledgeList;                
    int vertexSize; 
    graph() {
        this->vertexSize = 0;
        this->TotalvertexList = new DoublyVertexLinkedList();
        this->TotaledgeList = new DoublyEdgeLinkedList();
    }
    bool isFindVertex(int data) {       
        vertex* tempVertex;
        bool flag = false;
        tempVertex = TotalvertexList->head;
        while (tempVertex != NULL) {
            if (tempVertex->data == data)
            {
                flag = true; break;
            }
            tempVertex = tempVertex->next;
        }
        return flag;
    }
    vertex* findVertex(int data) {          
        vertex* tempVertex;
        tempVertex = TotalvertexList->head;
        while (tempVertex != NULL) {
            if (tempVertex->data == data)
            {
                break;
            }
            tempVertex = tempVertex->next;
        }
        return tempVertex;
    }
    bool isFindEdge(int source, int destination) {  
        edge* tempEdge;
        bool flag = false;
        tempEdge = TotaledgeList->head;
        while (tempEdge != NULL) {
            if ((tempEdge->source->data == source && tempEdge->destination->data == destination) ||
                (tempEdge->source->data == destination && tempEdge->destination->data == source))
            {
                flag = true;
                break;
            }
            tempEdge = tempEdge->next;
        }
        return flag;
    }

    void insert_vertex(int n) {                     
        if (vertexSize != 0) {
            if (isFindVertex(n) == true)return;
            else {
                vertex* newVertex = new vertex(n);
                TotalvertexList->insert(newVertex);
                this->vertexSize++;
            }
        }
        else {
            vertex* newVertex = new vertex(n);
            TotalvertexList->insert(newVertex);
            this->vertexSize++;
        }
    }

    void insert_edge(int source, int destination) {
        if (isFindVertex(source) == true && isFindVertex(destination) == true) {
            vertex* srcVertex = findVertex(source);
            vertex* dstVertex = findVertex(destination);
            edge* newEdge = new edge(srcVertex, dstVertex);     

            TotaledgeList->insert(newEdge);

            edge* tempEdge1 = new edge(srcVertex, dstVertex);       
            edge* tempEdge2 = new edge(srcVertex, dstVertex);       
            tempEdge1->myself_InTotal_EdgeList = newEdge;           
            tempEdge2->myself_InTotal_EdgeList = newEdge;           

            srcVertex->incidentEdgeList->insert(tempEdge1);
            dstVertex->incidentEdgeList->insert(tempEdge2);
            newEdge->myself_InFisrt_incidentEdge = tempEdge1;
            newEdge->myself_InSecond_incidentEdge = tempEdge2;

            srcVertex->increase_degree();
            dstVertex->increase_degree();
        }
        else {
            return;
        }
    }

    void DFS(int data) {
        vertex* curV = findVertex(data);
        curV->visited = true;
        cout << curV->data << " ";
        edge* temp = curV->incidentEdgeList->head;
        while (temp != NULL) {
            if (temp->discovery == false || temp->back == false) {
                vertex* w;
                if (temp->destination == curV)
                    w = temp->source;
                else {
                    w = temp->destination;
                }
                if (w->visited == true) {
                    temp->back = true;
                    temp = temp->next;
                }
                else {
                    temp->discovery = true;
                    DFS(w->data);
                    temp = temp->next;
                }
            }
            else {
                temp = temp->next;
            }
        }
    }
};
int main() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int N, M, K;
    int temp1, temp2;
    cin >> N >> M >> K;

    graph* g = new graph();

    for (int i = 0; i < N; i++) {
        cin >> temp1;
        g->insert_vertex(temp1);

    }

    for (int i = 0; i < M; i++) {
        cin >> temp1 >> temp2;
        if (g->isFindEdge(temp1, temp2) == true)cout << -1 << "\n";
        else g->insert_edge(temp1, temp2);
    }

    g->DFS(K);


}

问题: 根据相邻列表和使用DFS一次浏览所有顶点和交点的程序,创建消声图。 将顶点和互连性插入到图形中并执行DFS。如果在DFS运行之间可以从任何顶点A访问多个顶点,则将首先浏览在创建图形时首先连接到顶点A的顶点。主线以图中存在的两个顶点给出。如果两个顶点之间已经存在连接,则打印1。给出起点,从起点开始执行DFS,并按访问的顶点的顺序打印这些顶点的信息。

输入 第一行给出顶点数N(1≤1,000),主干行数M11≤M≤1000 00,DFS起始顶点数A。

在第二行中,通过分隔I11≤II≤20,000之间的空格来输入要插入图中的N的顶点数 随后,通过M线给出两个峰值S,D 11-98S,D≤20,000作为中继线信息。

输出 执行DFS时,请按照顶点的访问顺序打印出顶点信息。

输入实例:

5 8 18

5 18 25 52 97

5 18

5 25

18 25

5 52

18 5

25 52

5 97

25 97

输出示例:

-1

18 5 25 52 97

我编写的代码可以通过示例输出,但是在计分程序中发生了称为TIMELIMIT的错误。 我不知道如何处理此错误,因为这是我第一次看到问题。请帮助我.``

0 个答案:

没有答案
相关问题