Algorithm ekak

chirart1

Well-known member
  • Jul 27, 2009
    3,445
    468
    83
    Screenshot 2022-11-13 at 11.54.41.png

    mata one meke eka eka node wala anith node wala influence eka calculate karanna
    influence calculate karana formula eka
    = node value * influence factor * (1/hop count)
    api hithamu node values
    1 = 10
    2 = 20
    3 = 40
    4 = 10
    5 = 20

    1 consider karoth
    3 direct connect
    [2,5] 3 harha connect wenawa 1 ta
    so influence value eka 2n = 20 * influence factor * ( 1/ hop count ) 3 haraha nisa hop count eka 1

    4 consider karoth
    1 direct connect
    3 indirect connect 1 harha
    2 indirect connect 3 awilla 1 haraha
    5 indirect connect 3 awilla 1 haraha

    so influence value eka 2n = 20 * influence factor * (1/hop count) 3i 1i haraha connect wena nisa hop count eka 2i

    onna ohoma thiyena node okkogema anith node walin ena influence eka calculate karanna one :D
    any ideas?
     

    ThisaraMalintha

    Well-known member
  • Nov 16, 2015
    8,612
    7,331
    113
    ගෙදර
    dfs or bfs use karala paths tika traverse karaddi counter ekak use karala hop counts tika hoya ganna

    basic solution ekak hadala problem ekath ekka familiar unata passe optimize karanna ona widi oluwata ei.
     
    • Like
    Reactions: siri_ayya

    NAdee56

    Well-known member
  • Feb 24, 2009
    582
    310
    63
    anywhere
    oya algo ekata namak nadda ban? E.W. Dijkstra's Shortest Path algorithm eka dala balapan path eka hoyanna anith tika develop karanna puluwan
     
    • Like
    Reactions: EKGuest

    imhotep

    Well-known member
  • Mar 29, 2017
    14,825
    8
    35,339
    113
    The general mathematical approach to these type of problems is the Djikstra's algorithm.
    Read the material available from this link. BFS, DFS, Dijkstra.
    You might get an idea on how to implement it.
     
    • Like
    Reactions: EKGuest

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    මේ ප්‍රශ්නේ ටිකක් සංකීර්ණ දේකට තියෙන්නේ Hop Count එක හොයන එක. Dijkstra's Algorithm එකට වෙනස්කම් කිහිපයක් කරලා Hop Count එක හොයන්න පුලුවන්. ඒ වෙනස්කම් තමයි:

    1. Dijkstra's Algorithm එකේදී හොයන්නේ Source Node එකේ සිට graph එකේ අනිත් සියලුම Nodes වලට තියෙන කෙටිම දුර. නමුත් මේ ප්‍රශ්නේදී හොයන්නේ ඕකේ අනිත් පැත්ත. ඒ කියන්නේ දෙන ලද Node එකකට අනිත් සියලුම Nodes වල සිට ඇති Hop Count එක. ඒ නිසා Dijkstra's Algorithm එකේ අදාල තැන් වෙනස් කල යුතුයි.

    2. Dijkstra's Algorithm එකේදි nodes අතරේ දුර පෙන්වන්න graph එකක් පාවිච්චි කරනවා වගේ මේ ප්‍රශ්නේදී nodes අතරේ කනෙක්ෂන්ස් පෙන්වන්න Directed Graph එකක් පාවිච්චි කරන්න පුලුවන්. උදා: Node 2 සිට Node 3 දක්වා කනෙක්ෂන් එකක් තියෙන නිසා graph[2,3] = 1 යි නමුත් Node 3 සිට Node 2 දක්වා කනෙක්ෂන් එකක් නැති නිසා graph[3,2] = 0 යි.

    3. Dijkstra's Algorithm එකේ Node 0 පාවිච්චි කරනවා නමුත් මේ ප්‍රශ්නේ Node 0 පාවිච්චි කරන්නේ නැති නිසා Dijkstra's Algorithm එකේ අදාල තැන් වෙනස් කල යුතුයි.

    4. මේ ප්‍රශ්නේ Node එකකට තව Node එකක් ඩිරෙක්ට් කනෙක්ට් වෙනවානම් Hop Count එක 0 යි. අතරමැද Node 1 ක් හරහා කනෙක්ට් වෙනවානම් Hop Count එක 1 යි. අතර මැද Node 2ක් හරහානම් Hop Count එක 2 යි... ඔය විදියට යන්නේ. ඒක නිසා අපිට අවශ්‍ය Hop Count එක ගන්න Dijkstra's Algorithm එක ඇප්ලයි කරලා එන Hop Count එකෙන් එකක් අඩුකල යුතුයි.

    Hop Count එක හොයාගත්තට පස්සේ කරන්න තියෙන්නේ node value සහ influence factor එකත් එක්ක දිලා තියෙන ෆෝමියුලා එකට ආදේශ කරලා influence value එක calculate කරන එක.


    C#:
    using System;
    
    internal class Program
    {
    
        const int INFINITY = int.MaxValue;
           
        static int noOfNodes;
        static int[,] graph;
        static int mainNode;
        static decimal influenceFactor;
        static decimal[] nodeValues;
        static int[] hopCounts;
        static decimal[] influenceValues;
    
        static int[] FindHopCounts(int[,] graph, int mainNode)
        {
            int[] hopCounts = new int[noOfNodes + 1];
    
            bool[] sptSet = new bool[noOfNodes + 1];
    
            for (int i = 1; i <= noOfNodes; i++)
            {
                hopCounts[i] = INFINITY;
                sptSet[i] = false;
            }
    
            hopCounts[mainNode] = 0;
    
            for (int count = 1; count <= noOfNodes; count++)
            {
    
                int u = MinHopCountNode(hopCounts, sptSet);
    
                sptSet[u] = true;
    
                for (int v = 1; v <= noOfNodes; v++)
                {
                    if (!sptSet[v] && graph[v, u] != 0 &&
                            hopCounts[u] != INFINITY && hopCounts[u] + graph[v, u] < hopCounts[v])
                    {
                        hopCounts[v] = hopCounts[u] + graph[v, u];
                    }
                }
            }
    
            for (int i = 1; i <= noOfNodes; i++)
            {
                if (i != mainNode && hopCounts[i] != INFINITY)
                    hopCounts[i]--;
            }
    
            return hopCounts;
        }
           
        static int MinHopCountNode(int[] hopCounts,
                        bool[] sptSet)
        {
            int minValue = INFINITY, minIndex = -1;
    
            for (int v = 1; v <= noOfNodes; v++)
                if (sptSet[v] == false && hopCounts[v] <= minValue)
                {
                    minValue = hopCounts[v];
                    minIndex = v;
                }
    
            return minIndex;
        }
    
        static void CalculateInfluenceValues()
        {
            influenceValues = new decimal[noOfNodes + 1];
    
            for (int i = 1; i <= noOfNodes; i++)
            {
                if (i != mainNode)
                {
                    if (hopCounts[i] != INFINITY)
                    {
                        if (hopCounts[i] != 0)
                            influenceValues[i] = nodeValues[i] * influenceFactor * (1m / hopCounts[i]);
                        else
                            influenceValues[i] = decimal.MaxValue;
                    }
                }
            }
        }
    
        static void ReadInput()
        {
            Console.Write("Input number of nodes: ");
            noOfNodes = int.Parse(Console.ReadLine());
    
            Console.Write("Input main node: ");
            mainNode = int.Parse(Console.ReadLine());
    
            Console.Write("Input value for each node: ");
            string[] strNodeValues = Console.ReadLine().Split(' ');
            nodeValues = new decimal[noOfNodes + 1];
            for (int i = 1; i <= noOfNodes; i++)
                nodeValues[i] = int.Parse(strNodeValues[i - 1]);
    
            Console.Write("Input influence factor: ");
            influenceFactor = decimal.Parse(Console.ReadLine());
    
            Console.WriteLine("Input the graph of connected nodes:");
            graph = new int[noOfNodes + 1, noOfNodes + 1];
            for (int i = 1; i <= noOfNodes; i++)
            {
                string[] s = Console.ReadLine().Split(',');
                for (int j = 1; j <= noOfNodes; j++)
                {
                    graph[i, j] = int.Parse(s[j - 1]);
                }
            }
        }
    
        static void PrintResults(int[] hopCounts, int mainNode)
        {
            Console.WriteLine();
            for (int i = 1; i <= noOfNodes; i++)
            {
                if (i != mainNode)
                {
                    string strHopCount = "";
                    if (hopCounts[i] == INFINITY)
                        strHopCount = "No path";
                    else
                        strHopCount = hopCounts[i].ToString();
    
                    string strInfluenceValue = "";
                    if (hopCounts[i] == INFINITY)
                        strInfluenceValue = "No path";
                    else if (influenceValues[i] == decimal.MaxValue)
                        strInfluenceValue = "Infinity";
                    else
                        strInfluenceValue = influenceValues[i].ToString();
    
                    Console.WriteLine("Hop Count from {0} to {1}: {2}\t\tInfluence value from {3} to {4}: {5}",
                        i, mainNode, strHopCount, i, mainNode, strInfluenceValue);
                }
            }
        }
    
        static void Main(string[] args)
        {
            ReadInput();
    
            hopCounts = FindHopCounts(graph, mainNode);
            CalculateInfluenceValues();
    
            PrintResults(hopCounts, mainNode);
            Console.ReadLine();
        }
    
    }

    Example 1.png


    Example 2.png