fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace DailyProgrammer_NodeGraph
  6. {
  7. /* https://w...content-available-to-author-only...t.com/r/dailyprogrammer/comments/4ijtrt/20160509_challenge_266_easy_basic_graph/ */
  8. public class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var test = new NodeGraph();
  13. Console.WriteLine("Press enter to exit...");
  14. Console.ReadLine();
  15. }
  16. }
  17. public class Node
  18. {
  19. public List<Node> _edges = new List<Node>();
  20. public int degree { get; set; }
  21. public int NodeId { get; private set; }
  22. public Node(int nodeId)
  23. {
  24. NodeId = nodeId;
  25. }
  26. public int GetCountOfEdges()
  27. {
  28. return _edges.Count;
  29. }
  30. public bool Equals(int id)
  31. {
  32. return NodeId == id;
  33. }
  34. public bool Adjacent(Node n)
  35. {
  36. return _edges.Contains(n);
  37. }
  38. public void AddEdge(Node n)
  39. {
  40. n.degree += 1;
  41. degree += 1;
  42. _edges.Add(n);
  43. }
  44. public void Print()
  45. {
  46. Console.WriteLine("Node: {0} has a degree of {1}.", NodeId, degree);
  47. }
  48. }
  49. public class NodeGraph
  50. {
  51. private List<Node> Graph = new List<Node>();
  52. private int max;
  53. public NodeGraph()
  54. {
  55. GetInput();
  56. PrintAll();
  57. }
  58. public void AddEdgeToGraph(int edgeID, int nodeID)
  59. {
  60. var edge = Graph.Find(e => e.Equals(edgeID));
  61. if (edge == null) { return; }
  62. Graph.Find(q => q.Equals(nodeID)).AddEdge(edge);
  63. }
  64. private void GetInput()
  65. {
  66. var r = Console.ReadLine();
  67. max = int.Parse(r);
  68. InitNodes(max);
  69. var split = Console.ReadLine();
  70. while (split != "")
  71. {
  72. var text = split.Split(' ');
  73. var id1 = int.Parse(text[0]);
  74. var id2 = int.Parse(text[1]);
  75. AddEdgeToGraph(id2, id1);
  76. split = Console.ReadLine();
  77. }
  78. }
  79. private void InitNodes(int numNodes)
  80. {
  81. for (var i = 1; i <= numNodes; i++)
  82. {
  83. var n = new Node(i);
  84. Graph.Add(n);
  85. }
  86. }
  87. private void PrintAll()
  88. {
  89. foreach (var n in Graph) { n.Print(); }
  90. Console.WriteLine();
  91. foreach (var n in Graph)
  92. {
  93. for (var i = 0; i < max; i++)
  94. {
  95. int adjacent = 0;
  96. if (n.Adjacent(Graph[i])) { adjacent = 1; }
  97. if (Graph[i].Adjacent(n)) { adjacent = 1; }
  98. Console.Write("{0,3} ", adjacent);
  99. }
  100. Console.WriteLine();
  101. }
  102. }
  103. }
  104. }
Success #stdin #stdout 0.06s 24064KB
stdin
16
1 2
1 3
2 3
1 4
3 4
1 5
2 5
1 6
2 6
3 6
3 7
5 7
6 7
3 8
4 8
6 8
7 8
2 9
5 9
6 9
2 10
9 10
6 11
7 11
8 11
9 11
10 11
1 12
6 12
7 12
8 12
11 12
6 13
7 13
9 13
10 13
11 13
5 14
8 14
12 14
13 14
1 15
2 15
5 15
9 15
10 15
11 15
12 15
13 15
1 16
2 16
5 16
6 16
11 16
12 16
13 16
14 16
15 16

stdout
Node: 1 has a degree of 8.
Node: 2 has a degree of 8.
Node: 3 has a degree of 6.
Node: 4 has a degree of 3.
Node: 5 has a degree of 7.
Node: 6 has a degree of 10.
Node: 7 has a degree of 7.
Node: 8 has a degree of 7.
Node: 9 has a degree of 7.
Node: 10 has a degree of 5.
Node: 11 has a degree of 9.
Node: 12 has a degree of 8.
Node: 13 has a degree of 8.
Node: 14 has a degree of 5.
Node: 15 has a degree of 9.
Node: 16 has a degree of 9.

  0   1   1   1   1   1   0   0   0   0   0   1   0   0   1   1 
  1   0   1   0   1   1   0   0   1   1   0   0   0   0   1   1 
  1   1   0   1   0   1   1   1   0   0   0   0   0   0   0   0 
  1   0   1   0   0   0   0   1   0   0   0   0   0   0   0   0 
  1   1   0   0   0   0   1   0   1   0   0   0   0   1   1   1 
  1   1   1   0   0   0   1   1   1   0   1   1   1   0   0   1 
  0   0   1   0   1   1   0   1   0   0   1   1   1   0   0   0 
  0   0   1   1   0   1   1   0   0   0   1   1   0   1   0   0 
  0   1   0   0   1   1   0   0   0   1   1   0   1   0   1   0 
  0   1   0   0   0   0   0   0   1   0   1   0   1   0   1   0 
  0   0   0   0   0   1   1   1   1   1   0   1   1   0   1   1 
  1   0   0   0   0   1   1   1   0   0   1   0   0   1   1   1 
  0   0   0   0   0   1   1   0   1   1   1   0   0   1   1   1 
  0   0   0   0   1   0   0   1   0   0   0   1   1   0   0   1 
  1   1   0   0   1   0   0   0   1   1   1   1   1   0   0   1 
  1   1   0   0   1   1   0   0   0   0   1   1   1   1   1   0 
Press enter to exit...