You are on page 1of 6

Dijkstra Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace dijkstra
{
class Program
{
static int N;
static int[,] E;

static void Main(string[] args)


{
FileStream F = new FileStream("graph1.txt", FileMode.Open);
StreamReader SR = new StreamReader(F);

N = int.Parse(SR.ReadLine());
E = new int[N, N];

for (int i = 0; i < N; i++)


{
string S = SR.ReadLine();
string[] row = S.Split(',');

for (int j = 0; j < N; j++)


{
E[i, j] = int.Parse(row[j].Trim());
}

F.Close();
//---------------------------------------------------------------

int infinity = 99999;

int SourceNode = 1;

List<int> unprocessedNodes = new List<int>();

int[] d = new int[N];

for (int i = 0; i < N; i++)


{
d[i] = infinity;
unprocessedNodes.Add(i);
}

d[SourceNode] = 0;

//=====================================================

while (unprocessedNodes.Count > 0)


{
int SelectedNode = unprocessedNodes[0];
for (int i = 1; i < unprocessedNodes.Count; i++)
{
if (d[unprocessedNodes[i]]<d[SelectedNode])
{
SelectedNode = unprocessedNodes[i];
}
}
unprocessedNodes.Remove(SelectedNode);

for (int i = 0; i < N; i++)


{
if (E[SelectedNode, i] != -1)
{
if (d[SelectedNode] + E[SelectedNode, i] < d[i])
{
d[i] = d[SelectedNode] + E[SelectedNode, i];
}
}
}

for (int i = 0; i < N; i++)


{
Console.Write(" "+d[i]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Graph1.txt
5
-1,-1,-1,-1, 5
15, 2, 3, 5,-1
-1,-1,-1, 1, 2
-1,-1, 2,-1, 3
2, 1,-1, 1,-1

Shortest path in a graph


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ShortestPath
{
    class Path
    {
        private List<int> path;

        public Path(int startingNode)


        {
            path = new List<int>();
            path.Add(startingNode);
        }

        public void AppendPred(int Node)


        {
            path.Insert(0, Node);
        }

        public void AppendSucc(int Node)


        {
            path.Add(Node);
        }

        public int Length()


        {
            return path.Count - 1;
        }

        public void Print()


        {
            Console.Write("Length=" + Length() + "\t");
            for (int i = 0; i < path.Count; i++)
            {
                Console.Write(path[i]);
                if (i < path.Count - 1) Console.Write("-->");
            }
            Console.WriteLine();
        }
    }

    //=====================================================
    class Program
    {
        static int N;
        static int[,] E;

        static void Main(string[] args)


        {
            FileStream F = new FileStream("graph1.txt", FileMode.Open);
            StreamReader SR = new StreamReader(F);

            N = int.Parse(SR.ReadLine());
            E = new int[N, N];

            for (int i = 0; i < N; i++)


            {
                string S = SR.ReadLine();
                string[] row = S.Split(',');

                for (int j = 0; j < N; j++)


                {
                    if (row[j] == "0")
                    {
                        E[i, j] = 0;
                    }
                    else
                    {
                        E[i, j] = 1;
                    }
                }

            }
            F.Close();
            //---------------------------------------------------------------

            List<int> AllNodes = new List<int>();


            for (int i = 0; i < N; i++)
            {
                AllNodes.Add(i);
            }

            List<Path> PathList = FindAllPaths(1, 3, AllNodes);

            foreach (Path P in PathList)


            {
                P.Print();
            }
            Console.WriteLine("---------------------------------");

            int min=0;

            for (int i = 1; i < PathList.Count; i++)


            {
                if (PathList[i].Length() < PathList[min].Length())
                {
                    min = i;
                }
            }
            PathList[min].Print();
            
            Console.ReadKey();
        }

        //---------------------------------------------------------
        private static List<Path> FindAllPaths(int A, int B, List<int>
NodeList)
        {
            List<Path> L = new List<Path>();

            if (A == B)
            {
                L.Add(new Path(A));
            }
            else
            {
                int PosOfA = NodeList.IndexOf(A);
                NodeList.Remove(A);

                for (int i = 0; i < NodeList.Count; i++)


                {
                    int C = NodeList[i];
                    if (E[A, C] == 1)
                    {
                        List<Path> PathsFromCtoB = FindAllPaths(C, B,
NodeList);

                        foreach (Path P in PathsFromCtoB)


                        {
                            P.AppendPred(A);
                            L.Add(P);
                        }
                    }
                }

                NodeList.Insert(PosOfA, A);
            }
            return L;
        }
    }
}

Graph1.txt
5
0,1,0,1,0
1,0,1,0,1
0,1,0,1,0
1,0,1,0,1
0,1,0,1,0

You might also like