TSP贪心算法实现到某些图形示例

时间:2016-03-12 15:40:45

标签: algorithm graph

我试图找出这种图形结构的贪心算法?我该怎么办?

1.从随机节点开始

2.找到最近到未被访问过的邻居的路线(Ei vierailtu =未访问)

3.使邻居活跃

4.Mark主动节点被访问(vierailtu =已访问)

5.Save访问节点到路径

6.如果所有人都访问过 - >结束

7.如果没有去2。

这是我想在这里实现的算法,但我有点迷失。任何帮助都会得到满足。

class MainClass
{
    const int ALUSTAMATON_KAARI = 0;
    const int EI_KAARTA = -1;
    const int EI_KAARTA_ITSEEN = -2;

    const int VIERAILTU_SOLMU = -3;
    const int TUTKITTU_KAARI = -3;
    const int PALUUKAARI = -4;



    static int maxNodes = 11;


    public class Graph
    {

        private int[,]data;
        public int[,] Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }


        private int noNodes;
        public int NoNodes
        {
            get
            {
                return noNodes;
            }
            set
            {
                noNodes = value;
            }
        }


        public Graph()
        {
            data = new int[maxNodes,maxNodes];

            for (int i = 0; i < maxNodes; i++)
                for (int j = 0; j < maxNodes; j++)
                    if (i == j)
                        data[i,j] = EI_KAARTA_ITSEEN;
                    else
                        data[i,j] = EI_KAARTA;


            noNodes = 1;
        }
    }



    public static bool ADD_VERTEX(int v, Graph G)
    {
        if (G.NoNodes < maxNodes)
        {
            G.Data[G.NoNodes,0] = v;
            G.Data[0,G.NoNodes] = v;
            G.NoNodes++;
            return true;
        }
        else return false;
    }

    public static bool SET_VERTEX_VALUE(int v, int x, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v && i < G.NoNodes)
            i++;

        if (i < G.NoNodes)
        {
            G.Data[i, 0] = x;
            G.Data[0, i] = x;
            return true;
        }
        else return false;
    }

    public static bool SET_EDGE_VALUE(int v1, int v2, int x, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v1 && i < G.NoNodes)
            i++;

        int j = 0;
        if (i < G.NoNodes)
        {
            while (G.Data[j,0] != v2 && j < G.NoNodes)
                j++;
        }

        if (i < G.NoNodes && j < G.NoNodes)
        {
            if (G.Data[i, j] != EI_KAARTA && G.Data[j, i] != EI_KAARTA)
            {
                G.Data[i, j] = x;
                G.Data[j, i] = x;
                return true;
            }
            else return false;
        }
        else return false;
    }

    public static int GET_EDGE_VALUE(int v1, int v2, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v1 && i < G.NoNodes)
            i++;

        int j = 0;
        if (i < G.NoNodes)
        {
            while (G.Data[j,0] != v2 && j < G.NoNodes)
                j++;
        }

        if (i < G.NoNodes && j < G.NoNodes)
        {
            if (G.Data[i, j] != EI_KAARTA && G.Data[j, i] != EI_KAARTA)
            {
                return G.Data[i, j];
            }
            else return -1;
        }
        else return -1;
    }

    public static bool ADD_EDGE(int v1, int v2, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v1 && i < G.NoNodes)
            i++;

        int j = 0;
        if (i < G.NoNodes)
        {
            while (G.Data[j,0] != v2 && j < G.NoNodes)
                j++;
        }

        if (i < G.NoNodes && j < G.NoNodes)
        {
            G.Data[i,j] = ALUSTAMATON_KAARI;
            G.Data[j,i] = ALUSTAMATON_KAARI;
            return true;
        }
        else
            return false;
    }

    public static int[] NEIGHBORS(int v, Graph G)
    {
        int i = 0;
        int [] neighbors; 
        neighbors = new int[maxNodes];
        neighbors[0] = -2;

        while (G.Data[i,0] != v && i < G.NoNodes)
            i++;

        if (i < G.NoNodes)
        {
            for (int j = 1; j < maxNodes; j++)
                if(G.Data[i,j] >= 0)
                    neighbors[j] = 0;
                else
                neighbors[j] = EI_KAARTA;
        }
        return neighbors;
    }


    public static void PRINT_GRAPH(Graph G)
    {
        for (int i = 0; i < maxNodes; i++)
        {
            for (int j = 0; j < maxNodes; j++)
                Console.Write (G.Data[i,j] + " ");
            Console.WriteLine();
        }
        Console.WriteLine();
    }


    public static void MarkVisited(int v, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v && i < G.NoNodes)
            i++;

        if (i < G.NoNodes)
        {
            G.Data[i,i] = VIERAILTU_SOLMU;
        }
    }


    public static bool isVisited(int v, Graph G)
    {
        int i = 0;
        while (G.Data[i,0] != v && i < G.NoNodes)
            i++;

        if (i < G.NoNodes)
        {
            if (G.Data[i,i] == VIERAILTU_SOLMU)
                return true;
            else
                return false;
        }

        return false;
    }


    public static void INIT_DFS_PRINT(int v, Graph G)
    {
        Graph tmpG = new Graph();

        Array.Copy(G.Data, tmpG.Data, G.Data.Length);

        tmpG.NoNodes = G.NoNodes;

        DFS_PRINT(v, tmpG);

    }


    public static void DFS_PRINT(int v, Graph G)
    {
        int w;


        MarkVisited(v, G);
        Console.WriteLine("Vierailtu solmu: " + v);


        int[] tmp = new int[maxNodes];
        tmp = NEIGHBORS(v, G);

        for (int i = 0; i < maxNodes; i++)
        {
            //Console.WriteLine(tmp[i]);

            if (tmp[i] == 0)
            {

                int tmpValue = GET_EDGE_VALUE(v, G.Data[i, 0], G);

                if ( tmpValue >= 0 )
                {

                    w = G.Data[i, 0];

                    if (!isVisited(w, G))
                    {

                        SET_EDGE_VALUE(v, G.Data[i, 0], TUTKITTU_KAARI, G);


                        DFS_PRINT(w, G);
                    }
                    else

                        SET_EDGE_VALUE(v, G.Data[i, 0], PALUUKAARI, G);
                }
            }
        }
    }

    // Pääohjelma verkko-tietotyypin toteutuksen testaamista varten.
    public static void Main (string[] args)
    {

        Graph G = new Graph();

        PRINT_GRAPH(G);

        /
        ADD_VERTEX(5, G);
        ADD_VERTEX(9, G);
        ADD_VERTEX(12, G);
        ADD_VERTEX(3, G);


        ADD_EDGE(5, 9, G);
        ADD_EDGE(5, 3, G);
        ADD_EDGE(9, 12, G);

        PRINT_GRAPH(G);


        SET_VERTEX_VALUE(9, 11, G);
        SET_EDGE_VALUE(5,  3, 55, G);

        PRINT_GRAPH(G);


        int[] tmp = new int[maxNodes];
        tmp = NEIGHBORS(5, G);

        for (int i = 0; i < maxNodes; i++)
            Console.Write(tmp[i] + " ");
        Console.WriteLine();
        Console.WriteLine();


        INIT_DFS_PRINT(5, G);

    }
}

0 个答案:

没有答案