I've got newbie question, problem which I have to solve, to keep moving forward with my Unity project... It's probably pretty easy, but I'm not a programmer and a just don't know how to implement something I need. I checked documentation, but still don't know how to do it :/
I'm using graph class and I need to clone graph objects. Already know it's necessary to write deep clone function, coz I need NO references, just list of independent objects.
What I need is: create list of graphs. Change first one (by adding some edges), and clone it to the 2nd position on the list. Change 2nd (without changing 1st), and clone it to the 3rd position on the list, and so on...
This is my starting point:
public class Graph<T>
{
public int xMatrix;
public int yMatrix;
public int[] vertices;
public Graph() { }
public Graph(IEnumerable<T> vertices, IEnumerable<Tuple<T, T>> edges)
{
foreach (var vertex in vertices)
AddVertex(vertex);
foreach (var edge in edges)
AddEdge(edge);
}
public Dictionary<T, HashSet<T>> AdjacencyList { get; } = new Dictionary<T, HashSet<T>>();
public void AddVertex(T vertex)
{
AdjacencyList[vertex] = new HashSet<T>();
}
public void AddEdge(Tuple<T, T> edge)
{
if (AdjacencyList.ContainsKey(edge.Item1) && AdjacencyList.ContainsKey(edge.Item2))
{
AdjacencyList[edge.Item1].Add(edge.Item2);
AdjacencyList[edge.Item2].Add(edge.Item1);
}
}
}
And what I tried:
public class Graph<T> :ICloneable
{
public int xMatrix;
public int yMatrix;
public int[] vertices;
public IEnumerable<int> verts;
public IEnumerable<Tuple<int, int>> edgs;
public Graph() { }
public Graph(IEnumerable<T> vertices, IEnumerable<Tuple<T, T>> edges)
{
foreach (var vertex in vertices)
AddVertex(vertex);
foreach (var edge in edges)
AddEdge(edge);
verts = (IEnumerable<int>)vertices;
edgs = (IEnumerable<Tuple<int, int>>)edges;
}
public Dictionary<T, HashSet<T>> AdjacencyList { get; set; } = new Dictionary<T, HashSet<T>>();
public void AddVertex(T vertex)
{
AdjacencyList[vertex] = new HashSet<T>();
}
public void AddEdge(Tuple<T, T> edge)
{
if (AdjacencyList.ContainsKey(edge.Item1) && AdjacencyList.ContainsKey(edge.Item2))
{
AdjacencyList[edge.Item1].Add(edge.Item2);
AdjacencyList[edge.Item2].Add(edge.Item1);
}
//this.edgs.Append<edge>;
//IEnumerable<Tuple<int, int>> newEdgs = this.edgs.Append<Tuple<edge.Item1, edge.Item2>;
//IEnumerable<Tuple<int, int>> newEdgs = this.edgs.Append<edge>;
//???
}
public object Clone()
{
IEnumerable<int> vertices = this.verts;
IEnumerable<Tuple<int, int>> edges = this.edgs;
Graph<int> other = new Graph<int>(vertices, edges);
return other;
}
}
Thank you in advance!