/// <summary> /// Creates rows for the parent and child (if they don't already exist) and adds the child to the parent's list /// </summary> /// <param name="parent">The parent Vertex</param> /// <param name="child">The child Vertex to be added to the parent's Adjacency List</param> /// <param name="weight">The weigth of the adjacency (optional, defaults to zero)</param> public void AddEdge(Vertex parent, Vertex child, int weight = 0) { AdjacencyRow ParentRow = FindOrCreateAdjacencyRow(parent); AdjacencyRow ChildRow = FindOrCreateAdjacencyRow(child); FindOrAddAdjacencyItem(ParentRow, child, weight); FindOrAddAdjacencyItem(ChildRow, parent, weight); }
/// <summary> /// Returns all of the Edges connected to a Vertex as well as the edge's accociated weight /// </summary> /// <param name="parent">The Vertex to return all accociated edges</param> /// <returns>All edges accociated with the parent</returns> public Tuple <Vertex, int>[] GetNeighbors(Vertex parent) { AdjacencyRow row = AdjacencyList.Find(a => a.KeyVertex.Value == parent.Value); Tuple <Vertex, int>[] result = new Tuple <Vertex, int> [row.AdjacencyCount]; int i = 0; foreach (Tuple <Vertex, int> edge in row.Edges) { result[i] = edge; i++; } return(result); }
/// <summary> /// A utility function that will check if an adjacency already exists and will create one if it does not /// </summary> /// <param name="checkRow">The row to check for (and possibly add) the adjacency</param> /// <param name="child">The vertex to get added to the adjacency list</param> /// <param name="addWeight">The weight to add to the adjacency if it's added (defaults to zero)</param> private void FindOrAddAdjacencyItem(AdjacencyRow checkRow, Vertex child, int?addWeight) { bool ChildAlreadyExists = false; foreach (var pair in checkRow.Edges) { if (pair.Item1.Value == child.Value) { ChildAlreadyExists = true; } } if (!ChildAlreadyExists) { int weight = addWeight != null ? (int)addWeight : 0; checkRow.AddEdge(child, weight); } }
/// <summary> /// A utility function that checks for an existing Adjacency Row with the Vertex passed /// Or creates one if it doesn't exist /// </summary> /// <param name="vertex">The Vertex to check for or create in the Adjacency List</param> /// <returns>The Row that was found or created with the passed in vertex as its key</returns> private AdjacencyRow FindOrCreateAdjacencyRow(Vertex vertex) { AdjacencyRow RowWithKey = null; foreach (AdjacencyRow row in AdjacencyList) { if (row.KeyVertex.Value == vertex.Value) { RowWithKey = row; } } if (RowWithKey == null) { AddNewAdjacencyRow(vertex); RowWithKey = AdjacencyList.Find(v => v.KeyVertex.Value == vertex.Value); } return(RowWithKey); }
/// <summary> /// Overload for AddEdge that lets a user add an Vertex to the Graph without an adjacency /// </summary> /// <param name="parent">The Vertex to be added to the graph</param> public void AddEdge(Vertex parent) { AdjacencyRow ParentRow = FindOrCreateAdjacencyRow(parent); }