Graph
Basic knowledge is not introduced here, you can go to see <DSAA> or <algorithm> here is the C language implementation of the graph.
First of all, we want to "abstract" the diagram, specifically to find the key to describe the key information of the graph, here I simply select the vertex and edge, is the node and the path along the node.
The following is an implementation of the relationship between nodes based on the array description
Graph.h
/************************************************************code File:graph.hcode Writer:eofcode Date: 2014.11.22e-mail: [Email protected]code description:this file is a header file for out test program. We abstract The data structure-Graph here. And we alsodeclare some useful API to construct out naive graph:) ******************************************************* /#ifndef _graph_h_#define _graph_h_#include <stdio.h> #include <stdlib.h> #define CONNECTED Define disconnected 0#define SUCCESS 0#define FAILED -1struct graph{int num_vertex;int Num_edge;char adjacent[ 0];}; struct graph* init_graph (int vertex,int edge), void release_graph (struct graph* p_graph); int Add_edge (struct graph* P_graph,char From_v,char to_v); int print_graph (struct graph* p_graph); #endif
INIT_GRAPH.C the initialization of graph by this function
#include "graph.h" struct graph* init_graph (int vertex,int edge) {if (Vertex <=0 | | Edge <= 0) {return NULL;} int temp = 0;struct graph* p_graph = NULL; P_graph = (struct graph*) malloc (sizeof (struct graph) + Vertex*vertex);p _graph->num_vertex = vertex;p_graph->num_ Edge = edge;//initialize the adjacent relationshipfor (temp = 0;temp < vertex*vertex;temp++) {p_graph-> Adjacent[temp] = 0;} return p_graph;}
ADD_EDGE.C This function establishes a connectivity relationship for the From_v and To_v two nodes.
/************************************************************code File:add_edge.ccode Writer:eofcode Date: 2014.11.22e-mail: [Email protected]code description:this function would help us to add a new connectionbetween different ve Rtex which is in the graph.*************************************************************/#include "graph.h" int add_ Edge (struct graph* p_graph,char From_v,char to_v) {if (!p_graph | | From_v < 0 | | To_v < 0) {return-1;} * ((char*) (p_graph->adjacent) + from_v* (P_graph->num_vertex) + to_v) = 1;* ((char*) (p_graph-> Adjacent) + to_v* (P_graph->num_vertex) + from_v) = 1;return 0;}
RELEASE_GRAPH.C The final release diagram here.
/************************************************************code File:release_graph.ccode Writer:eofcode Date: 2014.11.22e-mail: [e-mail Protected]code description:it ' easy and convenient for us-to-call this API Onceand the graph.*************************************************************/#include "graph.h" #include "graph.h" void Release_graph (struct graph* p_graph) {if (!p_graph) {return;} Free (p_graph);}
Finally, the main program for testing.
#include <stdio.h> #include "graph.h" int main () {struct graph* p_graph = NULL; file* fp = fopen ("./text.txt", "r+"), if (!FP) {printf ("fopen () failed!\n"); return 0;} int ret = 0;int vertex = 0;int Edge = 0;int From_v = 0;int To_v = 0;fscanf (FP, "%d", &vertex); FSCANF (FP, "%d" , &edge);p _graph = Init_graph (vertex,edge); int temp = 0;for (Temp;temp < edge;temp++) {/***i think it ' s necessary to C Heck the returned value** of scanf () Family.*/ret = fscanf (FP, "%d%d", &from_v,&to_v); if (ret! = 2) {break;} Add_edge (P_GRAPH,FROM_V,TO_V);} Print_graph (p_graph); Release_graph (p_graph); fclose (FP); return 0;}
Test Material: Text file Text.txt
13130 54 30 19 126 45 40 211 129 100 67 89 115 3
Our test results are:
Data structure--an array implementation of graphs