答案 0 :(得分:1)
我已经有一段时间了,因为我已经完成了Dijkstra的算法实现,但您可以通过几种方式表示您的数据。例如,您可以拥有列表列表或列表数组。然后,您可以将数组索引视为顶点标签,将列表视为与其连接的数组列表。
public class Node
{
public int Weight { get; set; }
public int Connected { get; set; }
}
// You can use either an array of lists or a list of lists
List<List<Node>> graph = new List<List<Node>>();
// The index is the edge label - e.g. arr[0] is the edge labeled "0"
graph[0] = new List<Node>()
{
new Node() { Weight = 175, Connected = 1 },
new Node() { Weight= 100, Connected = 2 }
// Etc...
};
graph[1] = new List<Node>()
{
// Basically, to represent an undirected edge you're representing two weighted edges
// (i.e. a connection from 1 -> 2 and a connection from 2 -> 1)
// This also makes directed edges easy to represent
new Node() { Weight = 175, Connected = 1 }
// Etc...
};
例如,您还可以拥有顶点列表(或集合)和边缘列表。 (事实上,在正式的数学意义上,这是图的实际定义 - 一组边和一组顶点)。
答案 1 :(得分:1)
如果您正在寻找动态数组,请使用List&lt;&gt;宾语。我硬编码值,但您可以在需要动态对象时使用Add()方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
List<Node> graph = new List<Node>() {
new Node() {
id = 0, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 1,127),
new KeyValuePair<int,int>( 2,100),
new KeyValuePair<int,int>( 4,139),
new KeyValuePair<int,int>( 6,117),
new KeyValuePair<int,int>( 7,156)
}
},
new Node() {
id = 1, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 0,127),
new KeyValuePair<int,int>( 2,102),
new KeyValuePair<int,int>( 3,108),
new KeyValuePair<int,int>( 7,53)
}
},
new Node() {
id = 2, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 0,100),
new KeyValuePair<int,int>( 1,102),
new KeyValuePair<int,int>( 3,111),
new KeyValuePair<int,int>( 4,173),
new KeyValuePair<int,int>( 5,175)
}
},
new Node() {
id = 3, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 1,108),
new KeyValuePair<int,int>( 2,111)
}
},
new Node() {
id = 4, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 0,139),
new KeyValuePair<int,int>( 2,173),
new KeyValuePair<int,int>( 6,165)
}
},
new Node() {
id = 5, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 2,175),
new KeyValuePair<int,int>( 6,95),
new KeyValuePair<int,int>( 7,145)
}
},
new Node() {
id = 6, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 0,117),
new KeyValuePair<int,int>( 4,165),
new KeyValuePair<int,int>( 5,95)
}
},
new Node() {
id = 7, neighbors = new List<KeyValuePair<int,int>>() {
new KeyValuePair<int,int>( 0,156),
new KeyValuePair<int,int>( 1,53),
new KeyValuePair<int,int>( 5,145)
}
}
};
}
}
public class Node
{
public int id { get; set; }
public List<KeyValuePair<int, int>> neighbors { get; set; }
}
}
答案 2 :(得分:-1)
你可以简单地使用[] []:
double[][] x = new double[5][];
x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];
第二种选择是使用:
List<List<double>> 2darray = new List<List<double>> ();
2darray[0] = new List<double>();
2darray[0][0] = 1;