/* Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // C5 example: graph copying 2005-11-08 // Compile with // csc /r:C5.dll Toposort.cs using System; using System.Text; using C5; using SCG = System.Collections.Generic; namespace Graphcopy { class TestGraphcopy { public static void Main(String[] args) { if (args.Length != 1) Console.WriteLine("Usage: Graphcopy \n"); else { int count = int.Parse(args[0]); Node d = new Node("d"), e = new Node("e"), c = new Node("c", d, e), b = new Node("b", d), a = new Node("a", d, b, c); a[1] = a; Node newstart = CopyGraph1(a); Console.WriteLine("Copy has same structure: " + Isomorphic1(newstart, a)); Console.WriteLine("newstart = " + newstart); foreach (Node node1 in newstart.children) Console.WriteLine(node1); Node node = new Node("last"); for (int i=0; i(i.ToString(), node); newstart = CopyGraph1(node); Console.WriteLine("Copy has same structure: " + Isomorphic1(newstart, node)); } } // Graph copying 0 public static Node CopyGraph0(Node start) { IDictionary,Node> iso = new HashDictionary,Node> (ReferenceEqualityComparer>.Default); return CopyNode0(iso, start); } private static Node CopyNode0(IDictionary,Node> iso, Node old) { Node copy; if (!iso.Find(old, out copy)) { copy = new Node(old); iso[old] = copy; for (int i=0; i CopyGraph1(Node start) { IDictionary,Node> iso = new HashDictionary,Node> (ReferenceEqualityComparer>.Default); IStack> work = new ArrayList>(); iso[start] = new Node(start); work.Push(start); while (!work.IsEmpty) { Node node = work.Pop(), copy = iso[node]; for (int i=0; i child = node.children[i]; Node childCopy; if (!iso.Find(child, out childCopy)) { iso[child] = childCopy = new Node(child); work.Push(child); } copy.children[i] = childCopy; } } return iso[start]; } // Graph equality 0 public static bool Isomorphic0(Node start1, Node start2) { IDictionary,Node> iso = new HashDictionary,Node> (ReferenceEqualityComparer>.Default); return NodeEquals0(iso, start1, start2); } private static bool NodeEquals0(IDictionary,Node> iso, Node left, Node rght) { Node node; if (iso.Find(left, out node)) return Object.ReferenceEquals(node, rght); else if (left.children.Length != rght.children.Length) return false; else { iso[left] = rght; for (int i=0; i(Node start1, Node start2) { IDictionary,Node> iso = new HashDictionary,Node> (ReferenceEqualityComparer>.Default); IStack> work = new ArrayList>(); iso[start1] = start2; work.Push(start1); while (!work.IsEmpty) { Node left = work.Pop(), rght = iso[left]; if (left.children.Length != rght.children.Length) return false; else { for (int i=0; i lchild = left.children[i], rchild = rght.children[i]; Node node; if (iso.Find(lchild, out node)) { if (!Object.ReferenceEquals(node, rchild)) return false; } else { iso[lchild] = rchild; work.Push(lchild); } } } } return true; } } public class Node { public readonly T id; public readonly Node[] children; public Node(T id, params Node[] children) { this.id = id; this.children = children; } public Node(Node node) : this(node.id, new Node[node.children.Length]) { } public override String ToString() { StringBuilder sb = new StringBuilder(); sb.Append(id).Append("[ "); foreach (Node child in children) sb.Append(child.id).Append(" "); sb.Append("]"); return sb.ToString(); } public Node this[int i] { set { children[i] = value; } get { return children[i]; } } } }