Ejemplo n.º 1
0
// Adds a weighted edge between nodes u and v with the given weight.
void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight){
	if(u >= 0 && u < edgeList.size() && v >= 0 && v < edgeList.size() && u!= v && weight > 0){
		edgeList[u].push_back(NWPair(v,weight));
		edgeList[v].push_back(NWPair(u,weight));
		num_edges++;
	}
}
Ejemplo n.º 2
0
void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight){
	if(0 <= u < edgeList.size() && 0 <= v < edgeList.size() && u != v && weight > 0)
	{
		edgeList.at(u).push_back(NWPair(v, weight));
		edgeList.at(v).push_back(NWPair(u, weight));
		num_edges++;
	}
}
Ejemplo n.º 3
0
/*
   * Add a weighted, undirected edge between nodes u and v.
   * 
   * Preconditions: 
   *     u and v are legal labels (i.e. 0 <= u < G.size(), 0 <= v < G.size())
   *     u != v
   *     There is no edge between u and v.
   *     weight > 0
   */
   void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight) {
		EList::const_iterator it;
		NWPair check;
		if(u>=0 && u < edgeList.size() && v>=0 && v < edgeList.size() && u!=v){
			for(it = edgeList[u].begin(); it != edgeList[u].end();it++){
				check =*it;
				if(check.first == v ){//if an edge already exists, we do nothing and exit
					return;
				}
			}//if the for loop found no edge, we make one
			if(weight>0){
				edgeList[u].push_back(NWPair(v,weight));
				edgeList[v].push_back(NWPair(u,weight));
				num_edges++;
			}
		}
  }
Ejemplo n.º 4
0
/*
   * Add a weighted, undirected edge between nodes u and v.
   * 
   * Preconditions: 
   *     u and v are legal labels (i.e. 0 <= u < G.size(), 0 <= v < G.size())
   *     u != v
   *     There is no edge between u and v.
   *     weight > 0
   */
void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight){
	Elist::const_iterator it;
	if( 0 <= u < edgeList.size() && 0 <= v < edgeList.size()){
		if(u != v){
			if(weight > 0){
				for(it = edgeList[u].begin(); it !=edgeList[u].end(); it++){
					if(edgeList[u]->first == v && edgeList[u]->second == weight){
						return false;
					}
				}
			}
		}
	}
	edgeList[u].push_back(NWPair(v,weight));
	edgeList[v].push_back(NWPair(u, weight));
	num_edges++;
}
Ejemplo n.º 5
0
  /*
   * Return a list of NodeID/EdgeWeight pairs describing the nodes adjacent to edge w.
   *
   * Preconditions: u is a legal label.
   */
  std::list<NWPair> MatrixGraph::getAdj(NodeID u) const {
	  //i don't understand why this doesn't work
	  std::list<NWPair> aList;
	  for(int i = 0; i<M[u].size() ; i++){
		  if(M[u][i] != 0){
			  aList.push_back(NWPair(i,M[u][i]));
		  }
	  }
	  return aList;
  }
Ejemplo n.º 6
0
std::list<NWPair> MatrixGraph::getAdj(NodeID u) const {
    std::list<NWPair> ret;
    std::vector<EdgeWeight>::iterator it;
    for (it = M.at(u).begin; it != M.at(u).end(); it++) {
        // Only add if it's not itself or distance is infinity
        if (*it != 0 && *it != -1)
            ret.push_back(NWPair(u, *it));
    }

    return ret;
}
Ejemplo n.º 7
0
std::list<NWPair> MatrixGraph::getAdj(NodeID u) const {
	std::list<NWPair> adjs = std::list<NWPair>();
	for (int i = 0; i < M.size(); i++) {
		// If this condition is met, there is a weight
		// therefore an adjacent point.
		if (M[u][i] != 0) {
			adjs.push_back(NWPair(i, M[u][i]));
		}
	}
	return adjs;
}
Ejemplo n.º 8
0
std::list<NWPair> ListGraph::getAdj(NodeID u) const
{
    EList temp;
    EList::const_iterator it;
    for(it = edgeList[u].begin(); it != edgeList[u].end(); it++)
    {
        NWPair theEdge = *it;
        if(theEdge.first != NULL)
            temp.push_back(NWPair(theEdge.first, theEdge.second));
    }
    return temp;

}
Ejemplo n.º 9
0
 std::list<NWPair> MatrixGraph::getAdj(NodeID u) const
 {
	 std::vector<EdgeWeight> clone = M.at(u);
	 //return a list of pairs (node# and weight of edge between it and u)
	 std::list<NWPair> results;
	 NWPair temp;
	 for (int i = 0; i < clone.size(); i++)
	 {
		 if(clone.at(i) > 0)
		 {
			results.push_front( NWPair(i, clone.at(i)));
		 }
	 }
	 return results;
 }
Ejemplo n.º 10
0
void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight)
{
    edgeList[u].push_back(NWPair(v, weight));
    edgeList[v].push_back(NWPair(u, weight));
    num_edges++;
}