• 拓扑排序(邻接矩阵)


    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <queue>
    #include <Windows.h>
    
    using namespace std;
    
    #define INFINITY INT_MAX
    #define MAX_VERTEX_NUM 20  //顶点最多个数
    #define LENGTH 5           //顶点字符长度
    
    //邻接矩阵
    typedef struct _Graph
    {
        int matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];  //邻接矩阵
        char vexs[MAX_VERTEX_NUM][LENGTH];           //顶点数组
        int vexnum;                                  //顶点个数
        int arcs;                                    //弧的个数
    }Graph;
    
    int LocateVex(const Graph & g, char name[LENGTH])
    {
        for (int i = 0; i < g.vexnum; i++)
        {
            if (0 == strcmp(g.vexs[i], name))
            {
                return i;
            }
        }
        return -1;
    }
    
    //图的建造
    void CreateGraph(Graph &g)
    {
        ifstream fcin(_T("graph.txt"));
        fcin>>g.vexnum;
        for (int i = 0; i < g.vexnum; i++)
        {
            for (int j = 0; j < g.vexnum; j++)
            {
                g.matrix[i][j] = INFINITY;
            }
        }
        for (int i = 0; i < g.vexnum; i++)
        {
            fcin>>g.vexs[i];
        }
        fcin>>g.arcs;
        char arcHead[LENGTH];
        char arcTail[LENGTH];
        int weight;
        for (int i = 0; i < g.arcs; i++)
        {
            memset(arcHead, 0, LENGTH);
            memset(arcTail, 0, LENGTH);
            fcin>>arcTail>>arcHead>>weight;
            int x = LocateVex(g, arcHead);
            int y = LocateVex(g, arcTail);
            g.matrix[y][x] = weight;
        }
    }
    
    //v的第一个邻接点
    int FirstAdjVex(const Graph &g, int v)
    {
        for (int i = 0; i < g.vexnum; i++)
        {
            if (g.matrix[v][i] != INFINITY)
            {
                return i;
            }
        }
        return -1;
    }
    
    //v相对于w的下一个邻接点
    int NextAdjVex(const Graph &g, int v, int w)
    {
        for (int i = w + 1; i < g.vexnum; i++)
        {
            if (g.matrix[v][i] != INFINITY)
            {
                return i;
            }
        }
        return -1;
    }
    
    //邻接矩阵的输出
    void PrintAdjVex(const Graph &g)
    {
        for (int i = 0; i < g.vexnum; i++)
        {
            for (int j = 0; j < g.vexnum; j++)
            {
                if (g.matrix[i][j] == INFINITY)
                {
                    cout<<"*"<<'\t';
                }
                else
                {
                    cout<<g.matrix[i][j]<<'\t';
                }
            }
            cout<<endl;
        }
        cout<<endl;
    }
    
    //************************************拓扑排序*************************************begin
    //查找入度为0的顶点
    void FindInDegree(Graph g, int indegree[])
    {
        for (int i = 0; i < g.vexnum; i++)
        {
            for (int j = 0; j < g.vexnum; j++)
            {
                if (g.matrix[i][j] != INFINITY)
                {
                    indegree[j]++;
                }
            }
        }
    }
    
    //拓扑排序
    bool TopologicalSort(Graph g)
    {
        int indegree[MAX_VERTEX_NUM] = {0};
        FindInDegree(g, indegree);
        queue<int> q;
        int i = 0;
        for (; i < g.vexnum; i++)
        {
            if (indegree[i] == 0)
            {
                q.push(i);
            }
        }
        int count = 0;
        while (!q.empty())
        {
            i = q.front();
            q.pop();
            count++;
            cout<<g.vexs[i]<<"  ";
            for (int j = 0; j < g.vexnum; j++)
            {
                if (g.matrix[i][j] != INFINITY)
                {
                    if (!(--indegree[j]))
                    {
                        q.push(j);
                    }
                }
            }
        }
        if (count < g.vexnum)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    //************************************拓扑排序************************************end
    
    //辅助函数,设置控制台的颜色
    void SetConsoleTextColor(WORD dwColor)
    {
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        if (INVALID_HANDLE_VALUE == handle)
        {
            return;
        }
        SetConsoleTextAttribute(handle, dwColor);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Graph graph;
        CreateGraph(graph);
        SetConsoleTextColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        cout<<"************************邻接矩阵**************************"<<endl;
        PrintAdjVex(graph);
        SetConsoleTextColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        cout<<"************************拓扑排序**************************"<<endl<<endl;
        TopologicalSort(graph);
        cout<<endl<<endl;
    
        return 0;
    }

    界面运行如下:

    建造图所用的graph.txt如下:

    8
    V1 V2 V3 V4 V5 V6 V7 V8 
    10
    V1 V2 10
    V1 V3 50
    V2 V4 30
    V3 V5 40
    V3 V6 99
    V4 V5 2
    V4 V7 60
    V5 V7 80
    V6 V8 22
    V7 V8 70
  • 相关阅读:
    Spring的bean管理(注解)
    IOC 和DI的区别
    java spring属性注入
    Java 接口
    JAVA 修饰符
    Day19-File操作-创建 删除,文件过滤
    Eclipse快捷键大全
    Day17总结
    启动人人项目遇到的问题
    Eclipse 各版本号
  • 原文地址:https://www.cnblogs.com/venow/p/2641950.html
Copyright © 2020-2023  润新知