Advertisement
jwow22

Graph

Jan 5th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. public class Graph
  4. {
  5.    
  6.     private List<Vertex> vertices;
  7.     private HashSet<Edge> edges;
  8.    
  9.     public int Order => vertices.Count;
  10.     public int Size => edges.Count;
  11.  
  12.     public GraphRenderer Renderer;
  13.     public Vertex[] Vertices => vertices.ToArray();
  14.  
  15.     public Graph()
  16.     {
  17.         vertices = new List<Vertex>();
  18.         edges = new HashSet<Edge>();
  19.         Renderer = new GraphRenderer(this);
  20.     }
  21.        
  22.     public Vertex AddVertex(Vertex vertex)
  23.     {
  24.         vertices.Add(vertex);
  25.         return vertex;
  26.     }
  27.  
  28.     public void RemoveVertex(Vertex vertex)
  29.     {
  30.         RemoveAllEdges(vertex);
  31.         vertices.Remove(vertex);
  32.     }
  33.  
  34.     public void AddEdge(Vertex v1, Vertex v2)
  35.     {
  36.         edges.Add(v1.AddEdge(v2));
  37.         edges.Add(v2.AddEdge(v1));
  38.     }
  39.  
  40.     public void RemoveEdge(Vertex v1, Vertex v2)
  41.     {
  42.         v1.RemoveEdge(v2);
  43.         v2.RemoveEdge(v1);
  44.     }
  45.  
  46.     public void RemoveAllEdges(Vertex vertex)
  47.     {
  48.         foreach (Vertex connectedVertex in vertex.GetConnectedVertices())
  49.         {
  50.             RemoveEdge(vertex, connectedVertex);
  51.         }
  52.     }
  53.  
  54.     public Vertex AddConnectedVertex(Vertex v1, Vertex v2)
  55.     {
  56.         AddVertex(v2);
  57.         AddEdge(v1, v2);
  58.         return v2;
  59.     }
  60. }
  61.  
  62. public class Edge
  63. {
  64.     private Vertex source;
  65.     private Vertex destination;
  66.  
  67.     public EdgeRenderer Renderer;
  68.    
  69.     public Vertex Source => source;
  70.     public Vertex Destination => destination;
  71.  
  72.     public Edge(Vertex source, Vertex destination)
  73.     {
  74.         this.source = source;
  75.         this.destination = destination;
  76.  
  77.         Renderer = new EdgeRenderer(this);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement