我怎么给代表在C#中邻接表图? [英] How do I represent a graph given as an adjacency list in C#?

查看:176
本文介绍了我怎么给代表在C#中邻接表图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要各种图形算法程序,并作为输入,我已经给邻接表的形式图

I am going to program various graph algorithms, and as input, I have given graphs on the form of adjacency lists.

下面是一个例子:

1 2 3 4

2 1 3 4

3 1 2 4

4 1 2 3 5

4 1 2 3 5

5 4 6

6 5

该图具有6个顶点,由6行(每一行的第一个条目表示表示顶点的代表该行数)。在一行中的其余项表示对应于该行的顶点相邻的顶点。

The graph has 6 vertices, represented by 6 rows (and the first entry of each row denotes the number of the vertex representing that row). The remaining entries in a row denote the vertices that the vertex corresponding to that row are adjacent to.

什么是C#表示此的好办法?每一行中的条目数量不同似乎排除了一个数组,所以我找到了这些名单

What is a good way of representing this in C#? The differing number of entries in each row seem to rule out an array, so I found these lists of lists.

我要操纵图形,如: 合同边缘,我正在寻找一个数据结构,其中图形操作都是可能的和有效的。

I am going to manipulate the graph, e.g. contract edges, and I am looking for a data structure in which graph manipulations are both possible and efficient.

推荐答案

看起来像一个整数列表作为价值结构冷是有用的字典:

Looks like dictionary with a list of integers as a value structure cold be useful:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary<int, List<int>> graph = new Dictionary <int, List<int>>();
        graph[1] = new List<int> {2, 3, 4};
        graph[2] = new List<int> {1, 3, 4};
        graph[3] = new List<int> {1, 2, 4};
        graph[4] = new List<int> {1, 2, 3, 5};
        graph[5] = new List<int> {4, 6};
        graph[6] = new List<int> {5};
    }
}

这篇关于我怎么给代表在C#中邻接表图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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