/// <summary> /// Given a node this method first checks to see if the node has been added previously but /// is now inactive. If it is, it is reactivated. /// /// If the node has not been added previously, it is checked to make sure its index matches /// the next node index before being added to the graph. /// </summary> /// <param name="node">The node.</param> /// <returns>The node index.</returns> public int AddNode(Node node) { if (node.Index < Nodes.Count) { // make sure the client is not trying to add a node with the same // Id as a currently active node if (!Node.IsInvalidIndex(Nodes[node.Index].Index)) { Debug.LogError( "SparseGraph.AddNode: Attempting to add a node with a duplicate Id."); throw new System.Exception( "SparseGraph.AddNode: Attempting to add a node with a duplicate Id."); } Nodes[node.Index] = node; return(NextNodeIndex); } // make sure the new node has been indexed correctly if (node.Index != NextNodeIndex) { Debug.LogError("SparseGraph.AddNode: invalid index."); throw new System.Exception("SparseGraph.AddNode: invalid index."); } Nodes.Add(node); Edges.Add(new LinkedList <Edge>()); return(NextNodeIndex++); }
/// <summary> /// Method for obtaining a reference to a specific edge. /// </summary> /// <param name="from">The from index.</param> /// <param name="to">The two index.</param> /// <returns>The edge between the given node indices.</returns> public Edge GetEdge(int from, int to) { if (from >= Nodes.Count || from < 0 || Node.IsInvalidIndex(Nodes[from].Index)) { Debug.LogError("SparseGraph.GetEdge: invalid 'from' index."); throw new System.Exception("SparseGraph.GetEdge: invalid 'from' index."); } if (to >= Nodes.Count || to < 0 || Node.IsInvalidIndex(Nodes[to].Index)) { Debug.LogError("SparseGraph.GetEdge: invalid 'to' index."); throw new System.Exception("SparseGraph.GetEdge: invalid 'to' index."); } foreach (Edge edge in Edges[from]) { if (edge.To == to) { return(edge); } } Debug.LogError("SparseGraph.GetEdge: edge does not exist."); throw new System.Exception("SparseGraph.GetEdge: edge does not exist."); }
/// <summary> /// Iterates through all the edges in the graph and removes any that point to an invalidated /// node. /// </summary> public void CullInvalidEdges() { foreach (LinkedList <Edge> curEdgeList in Edges) { foreach (Edge curEdge in curEdgeList) { if (Node.IsInvalidIndex(Nodes[curEdge.To].Index) || Node.IsInvalidIndex(Nodes[curEdge.From].Index)) { curEdgeList.Remove(curEdge); } } } }
/// <summary> /// Determines the number of active nodes present in the graph (this /// method's performance can be improved greatly by caching the value). /// </summary> /// <returns>The number of active nodes present in the graph.</returns> public int NumActiveNodes() { int numActiveNodes = 0; for (int n = 0; n < Nodes.Count; ++n) { if (!Node.IsInvalidIndex(Nodes[n].Index)) { ++numActiveNodes; } } return(numActiveNodes); }
/// <summary> /// Use this to add an edge to the graph. The method will ensure that the edge passed as a /// parameter is valid before adding it to the graph. If the graph is a digraph then a /// similar edge connecting the nodes in the opposite direction will be automatically added. /// </summary> /// <param name="edge">The edge.</param> public void AddEdge(Edge edge) { // first make sure the from and to nodes exist within the graph if (edge.From >= NextNodeIndex || edge.To >= NextNodeIndex) { Debug.LogError("SparseGraph.AddEdge: invalid node index."); throw new System.Exception("SparseGraph.AddEdge: invalid node index."); } // make sure both nodes are active before adding the edge if (Node.IsInvalidIndex(Nodes[edge.To].Index) || Node.IsInvalidIndex(Nodes[edge.From].Index)) { return; } // add the edge, first making sure it is unique if (UniqueEdge(edge.From, edge.To)) { Edges[edge.From].AddLast(edge); } // if the graph is undirected we must add another connection // in the opposite direction if (IsDigraph) { return; } // check to make sure the edge is unique before adding if (!UniqueEdge(edge.To, edge.From)) { return; } var newEdge = new Edge(edge) { To = edge.From, From = edge.To }; Edges[edge.To].AddLast(newEdge); }
/// <summary> /// Tests if a node with the given index is present in the graph. /// </summary> /// <param name="nodeIndex">The node index.</param> /// <returns> /// True if a node with the given index is present in the graph. /// </returns> public bool IsNodePresent(int nodeIndex) { return(!Node.IsInvalidIndex(Nodes[nodeIndex].Index) && (nodeIndex < Nodes.Count)); }