如何在C#中为每个节点创建邻居列表? [英] How to can create list of neighbors for each node in C#?

查看:71
本文介绍了如何在C#中为每个节点创建邻居列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写dijkstra算法,我想为每个节点创建邻居列表我正在编写一些代码,但我的代码没有为每个节点创建邻居列表。我的代码有一个问题,它不会为每个节点区分邻居,这就是为什么要为每个节点保留完整的邻居列表。



图片 [ ^ ]



例如:节点[id] ==> (邻居,体重)

节点[ 0 ] == >  1  123 
节点[ 0 ] == > 2 113
节点[ 0 ] == > 3 214
节点[ 0 ] == > 4 203

节点[ 1 ] == > 2 175
节点[ 1 ] == > 4 214

< br $> b $ b

  public   class 
{
public 字典< int,List< KeyValuePair< int,int>>> vertices = new 字典< int,List< KeyValuePair< int,int>>>();

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

私人图g = new 图();

public int ID {获得; set ; }
private 列表< KeyValuePair< int,int>> dic = new 列表< KeyValuePair< int,int>>();

private void pictureBox1_MouseClick(对象 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 =结果;
}
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 ++;
}
}
其他
{
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();
}





我的尝试:



i想要为每个节点创建邻居列表

解决方案

嗯......



我建议阅读这篇MSDN文章:第5部分:来自树木到图表 [ ^ ],它可以帮助您了解如何从树创建图形。试试!

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)

Node[0] ==> (1,123)
Node[0] ==> (2,113)
Node[0] ==> (3,214)
Node[0] ==> (4,203)

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



 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

解决方案

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!


这篇关于如何在C#中为每个节点创建邻居列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆