void graph_remove_edge(graph *g, int edge_index) { void *edge_to_remove; if(g == NULL) { return; } /* make sure that these verticies actually exist */ if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == NULL) || (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == NULL)) { return; } edge_to_remove = graph_get_edge(g, edge_index); if(edge_to_remove == NULL) { return; } if(g->freeedge_fn != NULL) { g->freeedge_fn(edge_to_remove); } g->edges[edge_index] = NULL; g->nedges--; }
int graph_add_edge(graph *g, void *edge_data, int vertex_from, int vertex_to) { int edge_index; void *edge_data_copy; if(g == NULL) { return -1; } /* check to make sure there is a vertex at vertex_from, vertex_to */ if((graph_get_vertex(g, vertex_from) == NULL) || (graph_get_vertex(g, vertex_to) == NULL)) { return -1; } edge_index = row_col_to_1d(vertex_from, vertex_to, g->max_verticies); if(g->dupedge_fn != NULL) { edge_data_copy = g->dupedge_fn(edge_data); } else { edge_data_copy = edge_data; } if(g->edges[edge_index] != NULL) { /* if there was an edge there, remove it */ graph_remove_edge(g, edge_index); } g->edges[edge_index] = edge_data_copy; g->nedges++; return edge_index; }
void graph_add_edge(graphT *graph, char source, char dest) { vertexT *dst_vertex = graph_get_vertex(graph, dest); vertexT *src_vertex = graph_get_vertex(graph, source); edgeT *new_edge = malloc(sizeof(edgeT)); new_edge->connectsTo = dst_vertex; if(src_vertex->edges==NULL) new_edge->next=NULL; else new_edge->next=src_vertex->edges; src_vertex->edges=new_edge; }
void graph_remove_vertex(graph *g, int vertex) { int i; if(g == NULL) { return; } /* is vertex in scope? */ if(vertex >= g->max_verticies) { return; } if(g->verticies[vertex] == NULL) { return; } for(i = 0; i < g->max_verticies; i++) { /* remove the outgoing, then the incoming */ graph_remove_edge(g, row_col_to_1d(vertex, i, g->max_verticies)); graph_remove_edge(g, row_col_to_1d(i, vertex, g->max_verticies)); } /* did we manage vertex memory? */ if(g->freevertex_fn != NULL) { /* free the vertex itself */ g->freevertex_fn(graph_get_vertex(g, vertex)); } g->verticies[vertex] = NULL; g->nverticies--; }
void *graph_get_edge(graph *g, int edge_index) { if(g == NULL) { return NULL; } if((graph_get_vertex(g, graph_get_edge_src(g, edge_index)) == NULL) || (graph_get_vertex(g, graph_get_edge_dst(g, edge_index)) == NULL)) { return NULL; } if(edge_index >= (g->max_verticies)*(g->max_verticies)) { return NULL; } return g->edges[edge_index]; }
int GraphIsReachable(graphT *graph, char source, char destination){ //Set all the Vertex to not visited vertexT *temp = graph->vertices; while(temp!=NULL){ temp->visited=0; temp=temp->next; } vertexT *temp1 = graph_get_vertex(graph, source); return GraphIsReachableRecursive(temp1, destination); }
void printEdges(graphT *graph, char element) { printf("Element %c\n", element); vertexT *temp = graph_get_vertex(graph, element); if(temp==NULL){ printf("There's no Vertex with %c Element\n", element); exit(-1); } edgeT *edge = temp->edges; while(edge!=NULL){ printf("Connected to %c \n", edge->connectsTo->element); edge=edge->next; } }