// addEdge. // given valid from & to nodes, it adds an edge. // returns true in case of valid nodes, false otherwise public bool addEdge(UInt32 nID, UInt32 nFromID, UInt32 nToID, UInt32 nTraffic) { bool bRet = false; GraphNode from = null, to = null; if (!(isValidNode(nFromID)) || !(isValidNode(nToID))) { return bRet; } from = getNodeById(nFromID); to = getNodeById(nToID); GraphEdge e = new GraphEdge(nID, from, to, nTraffic); // check if from node is valid // check if to node is valid if (e.m_nodeFrom != null && e.m_nodeTo != null) { // calculate weight of the edge e.m_dWeight = Util.calcEuclidianDistance(e.m_nodeFrom, e.m_nodeTo); // add an edge. m_listEdges.AddSorted(e); //// also add the edges into from node. from.addEdge(e); //// also add the edges into to node to.addEdge(e); bRet = true; } return bRet; }
/// <summary> /// Generates all the possible edges for given nodes and returns them. /// </summary> /// <param name="listNodes">The list nodes.</param> /// <param name="listEdges">[out]The list edges.</param> /// <returns></returns> public static bool generateEdgeList(SortedNodeList listNodes, out SortedEdgeList listEdges) { bool bRet = false; SortedEdgeList l_listEdges = new SortedEdgeList(); UInt32 nEdgeID = 0; for(Int32 nFrom = 0; nFrom < listNodes.Count; ++nFrom) { for (Int32 nTo = nFrom+1; nTo < listNodes.Count; ++nTo) { // make sure it is sorted by distance. GraphEdge e = new GraphEdge(++nEdgeID, listNodes[nFrom], listNodes[nTo], 0); l_listEdges.AddSorted(e); bRet = true; } } listEdges = l_listEdges; return bRet; }
// /// <summary> /// adds edges uniquely to the edge list & sorted /// </summary> /// <param name="e">edge object</param> /// <returns> true if added successfully.</returns> public bool addEdge(GraphEdge e) { bool bRet = true; foreach (GraphEdge oe in m_listEdge) { if (oe.m_nID == e.m_nID) { bRet = false; } } if (bRet) m_listEdge.AddSorted(e); return bRet; }