using System;
using System.Collections.Generic;
using System.IO;
namespace DailyProgrammer_NodeGraph
{
/* https://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/4ijtrt/20160509_challenge_266_easy_basic_graph/ */
public class Program
{
static void Main(string[] args)
{
var test = new NodeGraph();
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
}
public class Node
{
public List<Node> _edges = new List<Node>();
public int degree { get; set; }
public int NodeId { get; private set; }
public Node(int nodeId)
{
NodeId = nodeId;
}
public int GetCountOfEdges()
{
return _edges.Count;
}
public bool Equals(int id)
{
return NodeId == id;
}
public bool Adjacent(Node n)
{
return _edges.Contains(n);
}
public void AddEdge(Node n)
{
n.degree += 1;
degree += 1;
_edges.Add(n);
}
public void Print()
{
Console.WriteLine("Node: {0} has a degree of {1}.", NodeId, degree);
}
}
public class NodeGraph
{
private List<Node> Graph = new List<Node>();
private int max;
public NodeGraph()
{
GetInput();
PrintAll();
}
public void AddEdgeToGraph(int edgeID, int nodeID)
{
var edge = Graph.Find(e => e.Equals(edgeID));
if (edge == null) { return; }
Graph.Find(q => q.Equals(nodeID)).AddEdge(edge);
}
private void GetInput()
{
var r = Console.ReadLine();
max = int.Parse(r);
InitNodes(max);
var split = Console.ReadLine();
while (split != "")
{
var text = split.Split(' ');
var id1 = int.Parse(text[0]);
var id2 = int.Parse(text[1]);
AddEdgeToGraph(id2, id1);
split = Console.ReadLine();
}
}
private void InitNodes(int numNodes)
{
for (var i = 1; i <= numNodes; i++)
{
var n = new Node(i);
Graph.Add(n);
}
}
private void PrintAll()
{
foreach (var n in Graph) { n.Print(); }
Console.WriteLine();
foreach (var n in Graph)
{
for (var i = 0; i < max; i++)
{
int adjacent = 0;
if (n.Adjacent(Graph[i])) { adjacent = 1; }
if (Graph[i].Adjacent(n)) { adjacent = 1; }
Console.Write("{0,3} ", adjacent);
}
Console.WriteLine();
}
}
}
}