邻接矩阵/邻接表的遍历方法

根据大话数据结构整理

/**********************************
*输入:邻接矩阵vMatrix/邻接表vList, 顶点表vNode
*输出:遍历vva元素
***********************************/

#include 
#include 
#include 
using namespace std;
#define INF 5000
/*******邻接矩阵深度优先遍历****************/
void DFSMatrix(vector<vector<int>> &vvEdge, vector<int > &vNode,
    vector<bool> &visit, int i)
{
    visit[i] = true;
    cout << vNode[i] << " ";
    for (unsigned j = 0; j < vNode.size(); j++)
        if (vvEdge[i][j] == 1 && !visit[j])
            DFSMatrix(vvEdge, vNode, visit, j);
    return;
}
void DFSAdjMatrix(vector<vector<int>> &vvEdge, vector<int > &vNode)
{
    vector<bool> visit(vNode.size() + 1);
    for (unsigned i = 0; i < vNode.size(); i++)
        if (!visit[i])
            DFSMatrix(vvEdge, vNode, visit, i);
    cout << endl;
    return;
}
/*******邻接表深度优先遍历****************/
void DFSList(vector<vector<int>> &vvEdge, vector<int > &vNode,
    vector<bool> &visit, int i)
{
    visit[i] = true;
    cout << vNode[i] << " ";
    for (unsigned j = 0; j < vvEdge[i].size(); j++)
    {
        int index = vvEdge[i][j];
        if (!visit[index])
            DFSList(vvEdge, vNode, visit, index);
    }
    return;
}
void DFSAdjList(vector<vector<int>> &vvEdge, vector<int > &vNode)
{
    vector<bool> visit(vNode.size() + 1);
    for (unsigned i = 0; i < vNode.size(); i++)
        if (!visit[i])
            DFSList(vvEdge, vNode, visit, i);
    cout << endl;
    return;
}
/**********邻接矩阵广度优先遍历******************/
void BFSAdjMatrix(vector<vector<int>> &vvEdge, vector<int > &vNode)
{
    vector<bool> visit(vNode.size() + 1);
    queue<int> Q;
    for (unsigned i = 0; i < vNode.size(); i++)
    {
        if (!visit[i])
        {
            visit[i] = true;
            cout << vNode[i] << " ";
            Q.push(i);
            while (!Q.empty())
            {
                i = Q.front();
                Q.pop();
                for (unsigned j = 0; j < vNode.size(); j++)
                {
                    if (vvEdge[i][j] == 1 && !visit[j])
                    {
                        visit[j] = true;
                        cout << vNode[j] << " ";
                        Q.push(j);
                    }
                }
            }
        }
    }
    cout << endl;
    return;
}
/**********邻接表广度优先遍历******************/
void BFSAdjList(vector<vector<int>> &vvEdge, vector<int > &vNode)
{
    vector<bool> visit(vNode.size() + 1);
    queue<int> Q;
    for (unsigned i = 0; i < vNode.size(); i++)
    {
        if (!visit[i])
        {
            visit[i] = true;
            cout << vNode[i] << " ";
            Q.push(i);
            while (!Q.empty())
            {
                i = Q.front();
                Q.pop();
                for (unsigned j = 0; j < vvEdge[i].size(); j++)
                {
                    int index = vvEdge[i][j];
                    if (!visit[index])
                    {
                        visit[index] = true;
                        cout << vNode[index] << " ";
                        Q.push(index);
                    }
                }
            }
        }
    }
    cout << endl;
    return;
}
/*********主函数****************/
int main()
{   
    int N, data1, data2;
    cin >> N;
    vector<int> v(N);   
    for (int i = 0; i < N; i++)
        cin >> v[i];
    cin >> N;
    /******建立邻接矩阵/邻接表**********/
    vector<vector<int>> vMatrix(v.size(), vector<int>(v.size(), INF));
    vector<vector<int>> vList(v.size());
    for(unsigned j = 0; j < v.size(); j++)
        vMatrix[j][j] = 0;
    for (int i = 0; i < N; i++)
    {
        cin >> data1 >> data2;
        vMatrix[data1][data2] = 1;
        vMatrix[data2][data1] = 1;
        vList[data1].push_back(data2);
        vList[data2].push_back(data1);
    }
    ///
    //for (int i = 0; i < vList.size(); i++)
    //{
    //  cout << i << ": ";
    //  for (int j = 0; j < vList[i].size(); j++)
    //      cout << vList[i][j] << " ";
    //  cout << endl;
    //}
    ///
    cout << "邻接矩阵深度优先遍历:" << endl;
    DFSAdjMatrix(vMatrix, v);
    cout << "邻接表深度优先遍历:" << endl;
    DFSAdjList(vList, v);
    cout << "邻接矩阵广度优先遍历:" << endl;
    BFSAdjMatrix(vMatrix, v);
    cout << "邻接表广度优先遍历:" << endl;
    BFSAdjList(vList, v);

    return 0;
}
/*case:顶点数n,n个顶点的编号;边数m,m条边*/
/*
9
1 2 3 4 5 6 7 8 9
15
0 1
0 5
1 2
1 8
1 6
2 3
2 8
3 4
3 7
3 6
3 8
4 5
4 7
5 6
6 7
*/

邻接矩阵/邻接表的遍历方法_第1张图片

你可能感兴趣的:(C++,算法)