Ejemplo n.º 1
0
void Network::setStringEdgeAttribute(vertex_id vid1, vertex_id vid2, const std::string& attribute_name, const std::string& val) {
	if (!containsVertex(vid1)) throw ElementNotFoundException("Vertex " + std::to_string(vid1));
	if (!containsVertex(vid2)) throw ElementNotFoundException("Vertex " + std::to_string(vid2));
	if (!containsEdge(vid1, vid2)) throw ElementNotFoundException("Edge (" + std::to_string(vid2) + ", " + std::to_string(vid2) + ")");
	if (edge_string_attribute.count(attribute_name)==0) throw ElementNotFoundException("Attribute " + attribute_name);
	edge_id eid(vid1, vid2, isDirected());
	edge_string_attribute.at(attribute_name)[eid] = val;
}
Ejemplo n.º 2
0
std::string Network::getStringEdgeAttribute(vertex_id vid1, vertex_id vid2, const std::string& attribute_name) const {
	if (!containsVertex(vid1)) throw ElementNotFoundException("Vertex " + std::to_string(vid1));
	if (!containsVertex(vid2)) throw ElementNotFoundException("Vertex " + std::to_string(vid2));
	if (!containsEdge(vid1, vid2)) throw ElementNotFoundException("Edge (" + std::to_string(vid1) + ", " + std::to_string(vid2) + ")");
	if (edge_string_attribute.count(attribute_name)==0) throw ElementNotFoundException("Attribute " + attribute_name);
	edge_id eid(vid1, vid2, isDirected());
	if (edge_string_attribute.at(attribute_name).count(eid)==0)  throw ElementNotFoundException("No attribute value for attribute " + attribute_name + " on edge (" + std::to_string(vid1) + "," +  std::to_string(vid2) + ")");
	return edge_string_attribute.at(attribute_name).at(eid);
}
Ejemplo n.º 3
0
bool Network::deleteEdge(vertex_id vid1, vertex_id vid2) {
	if (!containsEdge(vid1,vid2)) return false;
	edge_id eid(vid1,vid2,isDirected());
	out_edges[vid1].erase(vid2);
	in_edges[vid2].erase(vid1);
	if (!isDirected()) {
		out_edges[vid2].erase(vid1);
		in_edges[vid1].erase(vid2);
	}
	// remove attribute values (including WEIGHT, if present, which is treated as any other numeric attribute)
	for (std::map<std::string,std::map<edge_id,std::string> >::iterator it=edge_string_attribute.begin(); it!=edge_string_attribute.end(); it++)
		edge_string_attribute[it->first].erase(eid);
	for (std::map<std::string,std::map<edge_id,double> >::iterator it=edge_numeric_attribute.begin(); it!=edge_numeric_attribute.end(); it++)
		edge_numeric_attribute[it->first].erase(eid);
	num_edges--;
	return true;
}
Ejemplo n.º 4
0
void printMST(Vertex *root, FILE *outStream) {
    VList *forest;
    forest = NULL;
    EList *edges;
    edges = NULL;
    while (root != NULL) {
        VList *temp;
        temp = forest;
        if (!(forest = calloc(1, sizeof(VList)))) {
            fprintf(stderr, "Error: out of memory\n"); exit(1);
        }
        forest->name = strdup(root->name);
        //printf("V: %s\n", forest->name);
        forest->next = temp;
        Edge *ref;
        ref = root->edge;
        while (ref != NULL) {
            if (!containsEdge(edges, ref->to, ref->from)) {
                EList *tempE;
                tempE = edges;
                if (!(edges = calloc(1, sizeof(EList)))) {
                    fprintf(stderr, "Error: out of memory\n"); exit(1);
                }
                edges->to = strdup(ref->to);
                edges->from = strdup(ref->from);
                edges->weight = ref->weight;
                //printf("E: %s %s %d\n", edges->to, edges->from, edges->weight);
                edges->next = tempE;
            }
            ref = ref->next;
        }
        root = root->next;
    }
    //printf("That's all the unique vertices and edges");

    EList *mst;
    mst = NULL;

    while (edges != NULL && !spanning(mst, forest)) {
        EList *min;
        EList *temp;
        min = temp = edges;
        while (temp != NULL) {
            if (temp->weight < min->weight)
                min = temp;
            temp = temp->next;
        }
        if (min == edges) {
            edges = edges->next;
        }
        else {
            temp = edges;
            while (temp->next != min) {
                temp = temp->next;
            }
            temp->next = temp->next->next;
        }
        min->next = NULL;
        if (strcmp(min->from, min->to) == 0) {
            free(min->from);
            free(min->to);
            free(min);
        }
        else if (noWhereNew(mst, min->from, min->to)) {
            free(min->from);
            free(min->to);
            free(min);
        }
        else {
            temp = mst;
            mst = min;
            mst->next = temp;
            min = NULL;
        }
    }

    while (mst != NULL) {
        EList *temp;
        temp = mst;
        mst = mst->next;
        temp->next = NULL;
        fprintf(outStream, "%s %s\n", temp->from, temp->to);
        free(temp->from);
        free(temp->to);
        free(temp);
    }

    while (edges != NULL) {
        EList *temp;
        temp = edges;
        edges = edges->next;
        temp->next = NULL;
        free(temp->from);
        free(temp->to);
        free(temp);
    }

    while (forest != NULL) {
        VList *temp;
        temp = forest;
        forest = forest->next;
        temp->next = NULL;
        free(temp->name);
        free(temp);
    }
}
Ejemplo n.º 5
0
bool Network::containsEdge(const std::string& vertex_name1, const std::string& vertex_name2) const {
	if (!isNamed()) throw OperationNotSupportedException("Cannot reference a named edge in an unnamed network");
	if (!containsVertex(vertex_name1)) return false;
	if (!containsVertex(vertex_name2)) return false;
	return containsEdge(vertex_name_to_id.at(vertex_name1),vertex_name_to_id.at(vertex_name2));
}
Ejemplo n.º 6
0
/**
 * Adds a new edge index to this vertex.
 * @param i edge index
 */
void BOP_Vertex::addEdge(BOP_Index i) 
{
  if (!containsEdge(i))
    m_edges.push_back(i);
}