Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Puffin Rock Characters Plush Toy Set
anil1961
Updated:
Nov 7, 2026
Toyota Vitz scp10 For Sale
Lakmal Bandara
Updated:
Nov 4, 2026
Kandy
Welcome all my all new pet shop Bark.lk
dilumrane
Updated:
Nov 2, 2026
Windows RDP Only Rs.500 a month
Blogerwiki
Updated:
Nov 1, 2026
Ad icon
V2ray VPN for Tunneling
hu KANNA
Updated:
Oct 22, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
Computers & Internet
Software Development
Algorithm ekak
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="EKGuest" data-source="post: 28413322" data-attributes="member: 582168"><p>මේ ප්රශ්නේ ටිකක් සංකීර්ණ දේකට තියෙන්නේ Hop Count එක හොයන එක. Dijkstra's Algorithm එකට වෙනස්කම් කිහිපයක් කරලා Hop Count එක හොයන්න පුලුවන්. ඒ වෙනස්කම් තමයි:</p><p></p><p>1. Dijkstra's Algorithm එකේදී හොයන්නේ Source Node එකේ සිට graph එකේ අනිත් සියලුම Nodes වලට තියෙන කෙටිම දුර. නමුත් මේ ප්රශ්නේදී හොයන්නේ ඕකේ අනිත් පැත්ත. ඒ කියන්නේ දෙන ලද Node එකකට අනිත් සියලුම Nodes වල සිට ඇති Hop Count එක. ඒ නිසා Dijkstra's Algorithm එකේ අදාල තැන් වෙනස් කල යුතුයි.</p><p></p><p>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 යි.</p><p></p><p>3. Dijkstra's Algorithm එකේ Node 0 පාවිච්චි කරනවා නමුත් මේ ප්රශ්නේ Node 0 පාවිච්චි කරන්නේ නැති නිසා Dijkstra's Algorithm එකේ අදාල තැන් වෙනස් කල යුතුයි.</p><p></p><p>4. මේ ප්රශ්නේ Node එකකට තව Node එකක් ඩිරෙක්ට් කනෙක්ට් වෙනවානම් Hop Count එක 0 යි. අතරමැද Node 1 ක් හරහා කනෙක්ට් වෙනවානම් Hop Count එක 1 යි. අතර මැද Node 2ක් හරහානම් Hop Count එක 2 යි... ඔය විදියට යන්නේ. ඒක නිසා අපිට අවශ්ය Hop Count එක ගන්න Dijkstra's Algorithm එක ඇප්ලයි කරලා එන Hop Count එකෙන් එකක් අඩුකල යුතුයි.</p><p></p><p>Hop Count එක හොයාගත්තට පස්සේ කරන්න තියෙන්නේ node value සහ influence factor එකත් එක්ක දිලා තියෙන ෆෝමියුලා එකට ආදේශ කරලා influence value එක calculate කරන එක.</p><p></p><p></p><p>[CODE=csharp]using System;</p><p></p><p>internal class Program</p><p>{</p><p></p><p> const int INFINITY = int.MaxValue;</p><p> </p><p> static int noOfNodes;</p><p> static int[,] graph;</p><p> static int mainNode;</p><p> static decimal influenceFactor;</p><p> static decimal[] nodeValues;</p><p> static int[] hopCounts;</p><p> static decimal[] influenceValues;</p><p></p><p> static int[] FindHopCounts(int[,] graph, int mainNode)</p><p> {</p><p> int[] hopCounts = new int[noOfNodes + 1];</p><p></p><p> bool[] sptSet = new bool[noOfNodes + 1];</p><p></p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> {</p><p> hopCounts[i] = INFINITY;</p><p> sptSet[i] = false;</p><p> }</p><p></p><p> hopCounts[mainNode] = 0;</p><p></p><p> for (int count = 1; count <= noOfNodes; count++)</p><p> {</p><p></p><p> int u = MinHopCountNode(hopCounts, sptSet);</p><p></p><p> sptSet[u] = true;</p><p></p><p> for (int v = 1; v <= noOfNodes; v++)</p><p> {</p><p> if (!sptSet[v] && graph[v, u] != 0 &&</p><p> hopCounts[u] != INFINITY && hopCounts[u] + graph[v, u] < hopCounts[v])</p><p> {</p><p> hopCounts[v] = hopCounts[u] + graph[v, u];</p><p> }</p><p> }</p><p> }</p><p></p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> {</p><p> if (i != mainNode && hopCounts[i] != INFINITY)</p><p> hopCounts[i]--;</p><p> }</p><p></p><p> return hopCounts;</p><p> }</p><p> </p><p> static int MinHopCountNode(int[] hopCounts,</p><p> bool[] sptSet)</p><p> {</p><p> int minValue = INFINITY, minIndex = -1;</p><p></p><p> for (int v = 1; v <= noOfNodes; v++)</p><p> if (sptSet[v] == false && hopCounts[v] <= minValue)</p><p> {</p><p> minValue = hopCounts[v];</p><p> minIndex = v;</p><p> }</p><p></p><p> return minIndex;</p><p> }</p><p></p><p> static void CalculateInfluenceValues()</p><p> {</p><p> influenceValues = new decimal[noOfNodes + 1];</p><p></p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> {</p><p> if (i != mainNode)</p><p> {</p><p> if (hopCounts[i] != INFINITY)</p><p> {</p><p> if (hopCounts[i] != 0)</p><p> influenceValues[i] = nodeValues[i] * influenceFactor * (1m / hopCounts[i]);</p><p> else</p><p> influenceValues[i] = decimal.MaxValue;</p><p> }</p><p> }</p><p> }</p><p> }</p><p></p><p> static void ReadInput()</p><p> {</p><p> Console.Write("Input number of nodes: ");</p><p> noOfNodes = int.Parse(Console.ReadLine());</p><p></p><p> Console.Write("Input main node: ");</p><p> mainNode = int.Parse(Console.ReadLine());</p><p></p><p> Console.Write("Input value for each node: ");</p><p> string[] strNodeValues = Console.ReadLine().Split(' ');</p><p> nodeValues = new decimal[noOfNodes + 1];</p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> nodeValues[i] = int.Parse(strNodeValues[i - 1]);</p><p></p><p> Console.Write("Input influence factor: ");</p><p> influenceFactor = decimal.Parse(Console.ReadLine());</p><p></p><p> Console.WriteLine("Input the graph of connected nodes:");</p><p> graph = new int[noOfNodes + 1, noOfNodes + 1];</p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> {</p><p> string[] s = Console.ReadLine().Split(',');</p><p> for (int j = 1; j <= noOfNodes; j++)</p><p> {</p><p> graph[i, j] = int.Parse(s[j - 1]);</p><p> }</p><p> }</p><p> }</p><p></p><p> static void PrintResults(int[] hopCounts, int mainNode)</p><p> {</p><p> Console.WriteLine();</p><p> for (int i = 1; i <= noOfNodes; i++)</p><p> {</p><p> if (i != mainNode)</p><p> {</p><p> string strHopCount = "";</p><p> if (hopCounts[i] == INFINITY)</p><p> strHopCount = "No path";</p><p> else</p><p> strHopCount = hopCounts[i].ToString();</p><p></p><p> string strInfluenceValue = "";</p><p> if (hopCounts[i] == INFINITY)</p><p> strInfluenceValue = "No path";</p><p> else if (influenceValues[i] == decimal.MaxValue)</p><p> strInfluenceValue = "Infinity";</p><p> else</p><p> strInfluenceValue = influenceValues[i].ToString();</p><p></p><p> Console.WriteLine("Hop Count from {0} to {1}: {2}\t\tInfluence value from {3} to {4}: {5}",</p><p> i, mainNode, strHopCount, i, mainNode, strInfluenceValue);</p><p> }</p><p> }</p><p> }</p><p></p><p> static void Main(string[] args)</p><p> {</p><p> ReadInput();</p><p></p><p> hopCounts = FindHopCounts(graph, mainNode);</p><p> CalculateInfluenceValues();</p><p></p><p> PrintResults(hopCounts, mainNode);</p><p> Console.ReadLine();</p><p> }</p><p></p><p>}</p><p>[/CODE]</p><p></p><p>[ATTACH=full]191454[/ATTACH]</p><p></p><p>[ATTACH=full]191455[/ATTACH]</p></blockquote><p></p>
[QUOTE="EKGuest, post: 28413322, member: 582168"] මේ ප්රශ්නේ ටිකක් සංකීර්ණ දේකට තියෙන්නේ 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 කරන එක. [CODE=csharp]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(); } } [/CODE] [ATTACH type="full" alt="Example 1.png"]191454[/ATTACH] [ATTACH type="full" alt="Example 2.png"]191455[/ATTACH] [/QUOTE]
Insert quotes…
Verification
Asuwa dahayen wadi kalama keeyada?
Post reply
Top
Bottom