Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
            {
                circlesSourceAndDestination.Add(circleManager.Circles[result]);

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

                    dic.Add(new KeyValuePair<int, int>(result, weigth));
                    g.AddVertex(Id, dic);
                    //neighborList.Add(new KeyValuePair<int, int>(result, weigth));
                    //newNode.Neighbors = neighborList;

                    //graph.Add(newNode);

                    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 10:20am
Comments
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.

1 solution

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900