相邻列表中的JSON

本文关键字:JSON 列表 | 更新日期: 2023-09-27 18:28:23

我有一个这样的邻接列表:

A   -  A1
A   -  A2
A   -  A3
A3  - A31
A31 - A311
A31 - A312

我正在尝试获得以下输出:

{
    "name": "A",
    "children": [{
        "name": "A1"
    }, {
        "name": "A2"
    }, {
        "name": "A3",
        "children": [{
            "name": "A31",
            "children": [{
                "name": "A311"
            }, {
                "name": "A312"
            }]
        }]
    }]
};

我有一个包含100K链接的适度大的图表。做这件事的好方法是什么?我认为有一种非常优雅的递归方法可以做到这一点,但不确定如何直接创建JSON字符串。

相邻列表中的JSON

类似的东西应该起作用:

static void Main(string[] args)
{
    var adjList = new List<Link>
    {
        new Link("A","A1"),
        new Link("A","A2"),
        new Link("A","A3"),
        new Link("A3","A31"),
        new Link("A31","A311"),
        new Link("A31","A312"),
    };
    var rootsAndChildren = adjList.GroupBy(x => x.From)
           .ToDictionary(x => x.Key, x => x.Select(y => y.To).ToList());
    var roots = rootsAndChildren.Keys
           .Except(rootsAndChildren.SelectMany(x => x.Value));
    using (var wr = new StreamWriter("C:''myjson.json"))
    {
        wr.WriteLine("{");
        foreach (var root in roots)
            AppendSubNodes(wr, root, rootsAndChildren, 1);
        wr.WriteLine("};");
    }
}
static void AppendSubNodes(TextWriter wr, string root, 
          Dictionary<string, List<string>> rootsAndChildren, int level)
{
    string indent = string.Concat(Enumerable.Repeat(" ", level * 4));
    wr.Write(indent + "'"name'" : '"" + root + "'"");
    List<string> children;
    if (rootsAndChildren.TryGetValue(root, out children))
    {
        wr.WriteLine(",");
        wr.WriteLine(indent + "'"children'" : [{");
        for (int i = 0; i < children.Count; i++)
        {
            if (i > 0)
                wr.WriteLine(indent + "}, {");
            AppendSubNodes(wr, children[i], rootsAndChildren, level + 1);
        }
        wr.WriteLine(indent + "}]");
    }
    else
    {
        wr.WriteLine();
    }
}

Link为以下类别:

class Link
{
    public string From { get; private set; }
    public string To { get; private set; }
    public Link(string from, string to)
    {
        this.From = from;
        this.To = to;
    }
}

上一个代码的结果:

{
    "name" : "A",
    "children" : [{
        "name" : "A1"
    }, {
        "name" : "A2"
    }, {
        "name" : "A3",
        "children" : [{
            "name" : "A31",
            "children" : [{
                "name" : "A311"
            }, {
                "name" : "A312"
            }]
        }]
    }]
};

编辑:

如果您想检查图循环的存在,您可以执行以下操作(在创建rootsAndChildren字典之后)

var allNodes = rootsAndChildren.Keys.Concat(rootsAndChildren.SelectMany(x => x.Value)).Distinct();
Func<string, IEnumerable<string>> getSuccessors =
    (x) => rootsAndChildren.ContainsKey(x) ? rootsAndChildren[x] : Enumerable.Empty<string>();
var hasCycles = new Tarjan<string>().HasCycle(allNodes, getSuccessors);

Tarjan为以下类别:

// Please note that Tarjan does not detect a cycle due to a node 
// pointing to itself. It's pretty trivial to account for that though...
public class Tarjan<T>
{
    private class Node
    {
        public T Value { get; private set; }
        public int Index { get; set; }
        public int LowLink { get; set; }
        public Node(T value)
        {
            this.Value = value;
            this.Index = -1;
            this.LowLink = -1;
        }
    }
    private Func<T, IEnumerable<T>> getSuccessors;
    private Dictionary<T, Node> nodeMaps;
    private int index = 0;
    private Stack<Node> stack;
    private List<List<Node>> SCC;
    public bool HasCycle(IEnumerable<T> nodes, Func<T, IEnumerable<T>> getSuccessors)
    {
        return ExecuteTarjan(nodes, getSuccessors).Any(x => x.Count > 1);
    }
    private List<List<Node>> ExecuteTarjan(IEnumerable<T> nodes, Func<T, IEnumerable<T>> getSuccessors)
    {
        this.nodeMaps = nodes.ToDictionary(x => x, x => new Node(x));
        this.getSuccessors = getSuccessors;
        SCC = new List<List<Node>>();
        stack = new Stack<Node>();
        index = 0;
        foreach (var node in this.nodeMaps.Values)
        {
            if (node.Index == -1)
                TarjanImpl(node);
        }
        return SCC;
    }
    private IEnumerable<Node> GetSuccessors(Node v)
    {
        return this.getSuccessors(v.Value).Select(x => this.nodeMaps[x]);
    }
    private List<List<Node>> TarjanImpl(Node v)
    {
        v.Index = index;
        v.LowLink = index;
        index++;
        stack.Push(v);
        foreach (var n in GetSuccessors(v))
        {
            if (n.Index == -1)
            {
                TarjanImpl(n);
                v.LowLink = Math.Min(v.LowLink, n.LowLink);
            }
            else if (stack.Contains(n))
            {
                v.LowLink = Math.Min(v.LowLink, n.Index);
            }
        }
        if (v.LowLink == v.Index)
        {
            Node n;
            List<Node> component = new List<Node>();
            do
            {
                n = stack.Pop();
                component.Add(n);
            } while (n != v);
            SCC.Add(component);
        }
        return SCC;
    }
}