Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Collections;
- using System.Collections.ObjectModel;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- //TODO - PARSE INPUTS AND CREATE INPUT LIST
- List<Domino> input = new List<Domino>();
- input.Add(new Domino(3, 3));
- Console.Write(input[0].leftFace);
- Console.ReadLine();
- NodeList pregraph = new NodeList();
- for (int i = 0; i < input.Count; i++)
- {
- pregraph.Add(new Node(input[i]));
- }
- Console.Write(pregraph[0].Value.key);
- Console.ReadLine();
- Graph dominograph = new Graph(pregraph);
- dominograph.Remove(dominograph.Nodes[0]);
- Console.WriteLine(dominograph.Nodes.Count);
- Console.ReadLine();
- }
- }
- public class Domino
- {
- Random randm = new Random();
- public int leftFace { get; set; }
- public int rightFace { get; set; }
- public int key;
- public Domino(int left, int right)
- {
- this.leftFace = left;
- this.rightFace = right;
- key = randm.Next();
- }
- }
- public class Node
- {
- public Domino data;
- protected NodeList leftNeighbors = new NodeList
- { };
- protected NodeList rightNeighbors = new NodeList
- { };
- public Node() { }
- public Node(Domino input)
- {
- data = input;
- }
- public Node(Domino data, NodeList leftNeighbors, NodeList rightNeighbors)
- {
- this.data = data;
- this.leftNeighbors = leftNeighbors;
- this.rightNeighbors = rightNeighbors;
- }
- public Domino Value
- {
- get
- {
- return this.data;
- }
- set
- {
- data = Value;
- }
- }
- new public NodeList RightNeighbors
- {
- get
- {
- if (rightNeighbors == null)
- rightNeighbors = new NodeList();
- return rightNeighbors;
- }
- }
- new public NodeList LeftNeighbors
- {
- get
- {
- if (leftNeighbors == null)
- leftNeighbors = new NodeList();
- return rightNeighbors;
- }
- }
- }
- public class NodeList : Collection<Node>
- {
- public NodeList() : base() { }
- public NodeList(int initialSize)
- {
- // Add the specified number of items
- for (int i = 0; i < initialSize; i++)
- base.Items.Add(default(Node));
- }
- public Node FindByValue(Domino value)
- {
- // search the list for the value
- foreach (Node node in Items)
- if (node.Value.Equals(value))
- return node;
- // if we reached here, we didn't find a matching node
- return null;
- }
- }
- public class GraphNode : Node
- {
- private List<int> costs;
- public Node Data { get; set; }
- public GraphNode() : base() { }
- public GraphNode(Node blah)
- {
- Data = blah;
- }
- new public NodeList RightNeighbors
- {
- get
- {
- if (base.rightNeighbors == null)
- base.rightNeighbors = new NodeList();
- return base.rightNeighbors;
- }
- }
- new public NodeList LeftNeighbors
- {
- get
- {
- if (base.leftNeighbors == null)
- base.leftNeighbors = new NodeList();
- return base.rightNeighbors;
- }
- }
- public List<int> Costs
- {
- get
- {
- if (costs == null)
- costs = new List<int>();
- return costs;
- }
- }
- }
- public class Graph : IEnumerable<GraphNode>
- {
- private NodeList nodeSet;
- public Graph() : this(null) { }
- public Graph(NodeList nodeSet)
- {
- if (nodeSet == null)
- this.nodeSet = new NodeList();
- else
- this.nodeSet = nodeSet;
- }
- public void AddNode(GraphNode node)
- {
- // adds a node to the graph
- nodeSet.Add(node.Data);
- }
- public void AddNode(Node value)
- {
- // adds a node to the graph
- nodeSet.Add(new GraphNode(value));
- }
- public void AddUndirectedRightEdge(Node from, Node to, int cost)
- {
- from.RightNeighbors.Add(to);
- if((from.Value.rightFace == to.Value.rightFace))
- to.RightNeighbors.Add(from);
- else if ((from.Value.rightFace == to.Value.leftFace))
- to.LeftNeighbors.Add(from);
- }
- public void AddUndirectedLeftEdge(GraphNode from, GraphNode to, int cost)
- {
- from.LeftNeighbors.Add(to);
- if ((from.Value.leftFace == to.Value.rightFace))
- to.RightNeighbors.Add(from);
- else if ((from.Value.leftFace == to.Value.leftFace))
- to.LeftNeighbors.Add(from);
- }
- public bool Contains(Domino value)
- {
- return nodeSet.FindByValue(value) != null;
- }
- public bool Remove(Node node)
- {
- // first remove the node from the nodeset
- Node nodeToRemove = nodeSet.FindByValue(node.Value);
- if (nodeToRemove == null)
- // node wasn't found
- return false;
- // otherwise, the node was found
- nodeSet.Remove(nodeToRemove);
- // enumerate through each node in the nodeSet, removing edges to this node
- foreach (Node gnode in nodeSet)
- {
- int index = gnode.RightNeighbors.IndexOf(nodeToRemove);
- if (index != -1)
- {
- // remove the reference to the node and associated cost
- gnode.RightNeighbors.RemoveAt(index);
- }
- }
- foreach (Node gnode in nodeSet)
- {
- int index = gnode.RightNeighbors.IndexOf(nodeToRemove);
- if (index != -1)
- {
- // remove the reference to the node and associated cost
- gnode.RightNeighbors.RemoveAt(index);
- }
- }
- return true;
- }
- public IEnumerator<GraphNode> GetEnumerator()
- {
- throw new NotImplementedException();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- throw new NotImplementedException();
- }
- public NodeList Nodes
- {
- get
- {
- return nodeSet;
- }
- }
- public int Count
- {
- get { return nodeSet.Count; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment