15,851,387 members
See more:
I'm writing dijkstra algorithm,i want to create list of neighbors for each node i am writing a little bit code but my code doesn't created list of neighbors for each node. my code has a problem that doesn't distinct neighbors for each node and that's why keeps totally list of neighbors for each node.

Image[^]

for example: Node[id] ==> (neighbor,weight)
C#
Node[0] ==> (1,123)
Node[0] ==> (2,113)
Node[0] ==> (3,214)
Node[0] ==> (4,203)

Node[1] ==> (2,175)
Node[1] ==> (4,214)

C#
public class Graph
{
public Dictionary<int, List<KeyValuePair<int, int>>> vertices = new Dictionary<int, List<KeyValuePair<int, int>>>();

public void AddVertex(int id, List<KeyValuePair<int, int>> edges)
{
vertices[id] = edges;
}
}

private Graph g = new Graph();

public int Id { get; set; }
private List<KeyValuePair<int, int>> dic = new List<KeyValuePair<int, int>>();

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
var result = circleManager.HitTest(e.Location);
if (result != -1)
{

if (Count == 1)
{
Id = result;
}
else if (Count == 2)
{
var weigth = CalculateLengthSourceAndDestination(circlesSourceAndDestination);
circlesSourceAndDestination.Clear();
if (weigth < 0)
{
weigth *= -1;
}

//newNode.Neighbors = neighborList;

Count = 0;
}
Count++;
}
}
else
{
var result = circleManager.HitTest(e.Location);
if (result != -1)
{
circleManager.Circles[circleManager.HitTest(e.Location)].Selected = true;
circleManager.Circles[result].SelectFillColor = Color.Red;
}
}
pictureBox1.Invalidate();
}

What I have tried:

i want to create list of neighbors for each node
Posted
Updated 31-Aug-16 11:20am
Patrice T 31-Aug-16 14:14pm
Where in this code are you looking for neighbors.
How do you know the a node is neighbor of another ?
Philippe Mori 31-Aug-16 16:30pm
It is not clear what problem you have? Tell us what you get and what you expect.
Philippe Mori 1-Sep-16 8:33am
If you cannot properly explain what does not work with your code, it is hard to help you because we have to try to understand partial code having no idea what it presently does. It would be better for you to use a debugger and find the problem yourself if you are not able to provide us all information required for us to help you.

## Solution 1

Well...

I'd suggest to read this MSDN article: Part 5: From Trees to Graphs[^], which may help you to understand how to create graph from tree. Try!