以下给出了以邻接表表示图时的:图建立、图删除、深度优先遍历、广度优先遍历的代码。
#include<iostream>
#include<queue>
using namespace std;
struct edge //structure of edge
{
int vertex1_index;
int vertex2_index;
};
struct edge_node //structure of node on the edge list
{
int index;
edge_node* sibling;
};
struct vertex_node //structure of vertex
{
char c_value;
edge_node* first_child;
};
struct graph_adj_list_presentation //structrue of graph
{
int number_of_nodes;
int number_of_edges;
vertex_node* vertex_array;
};
graph_adj_list_presentation creat_graph(char c_values[],int number_of_vertexs,edge* edges,int number_of_edges) //function to create a graph
{
graph_adj_list_presentation graph;
graph.number_of_nodes=number_of_vertexs;
graph.number_of_edges=number_of_edges;
graph.vertex_array=(vertex_node*)malloc(number_of_vertexs*sizeof(vertex_node));
for(int i=0;i<number_of_vertexs;i++)
{
graph.vertex_array[i].c_value=c_values[i];
graph.vertex_array[i].first_child=NULL;
}
for(int i=0;i<number_of_edges;i++)
{
edge_node* temp_node_pointer=(edge_node*)malloc(sizeof(edge_node));
temp_node_pointer->index=edges[i].vertex2_index;
temp_node_pointer->sibling=graph.vertex_array[edges[i].vertex1_index].first_child;
graph.vertex_array[edges[i].vertex1_index].first_child=temp_node_pointer;
temp_node_pointer=(edge_node*)malloc(sizeof(edge_node));
temp_node_pointer->index=edges[i].vertex1_index;
temp_node_pointer->sibling=graph.vertex_array[edges[i].vertex2_index].first_child;
graph.vertex_array[edges[i].vertex2_index].first_child=temp_node_pointer;
}
return graph;
}
void delete_graph(graph_adj_list_presentation graph_deleted) //function to release the menmory hold by the graph
{
int number_of_vertexs=graph_deleted.number_of_nodes;
for(int i=0;i<number_of_vertexs;i++)
{
while(graph_deleted.vertex_array[i].first_child!=NULL)
{
edge_node* temp_pointer=graph_deleted.vertex_array[i].first_child;
graph_deleted.vertex_array[i].first_child=temp_pointer->sibling;
free(temp_pointer);
}
}
free(graph_deleted.vertex_array);
}
void dfsm(graph_adj_list_presentation graph_tranversed,int current_vertex,bool checked[]) //sub function used by the function dfs
{
if(checked[current_vertex]==true)
return;
cout<<graph_tranversed.vertex_array[current_vertex].c_value<<" ";
checked[current_vertex]=true;
edge_node* temp_pointer=graph_tranversed.vertex_array[current_vertex].first_child;
while(temp_pointer!=NULL)
{
dfsm(graph_tranversed,temp_pointer->index,checked);
temp_pointer=temp_pointer->sibling;
}
}
void dfs(graph_adj_list_presentation graph_tranversed) //deep first search the group
{
bool check[7]={false,false,false,false,false,false,false};
for(int i=0;i<graph_tranversed.number_of_nodes;i++)
{
dfsm(graph_tranversed,i,check);
}
}
void bfs(graph_adj_list_presentation graph_tranversed) //breadth first search the group
{
bool checked[]={false,false,false,false,false,false,false};
queue<int> queue_for_use;
for(int i=0;i<graph_tranversed.number_of_nodes;i++)
{
if(checked[i]==false)
{
queue_for_use.push(i);
checked[i]=true;
while(!queue_for_use.empty())
{
int top_value=queue_for_use.front();
queue_for_use.pop();
cout<<graph_tranversed.vertex_array[top_value].c_value<<" ";
edge_node* edge_node_temp_pointer=graph_tranversed.vertex_array[top_value].first_child;
while(edge_node_temp_pointer!=NULL)
{
if(!checked[edge_node_temp_pointer->index])
{
queue_for_use.push(edge_node_temp_pointer->index);
checked[edge_node_temp_pointer->index]=true;
}
edge_node_temp_pointer=edge_node_temp_pointer->sibling;
}
}
}
}
}
int main(int* argc,char* argv[])
{
graph_adj_list_presentation graph;
char c_node_values[7]={'a','b','c','d','e','f','g'};
edge edges[6]={{0,1},{0,2},{1,3},{2,3},{3,5},{2,4}};
graph=creat_graph(c_node_values,7,edges,6);
cout<<"adjacent list look like follos:"<<endl;
for(int i=0;i<graph.number_of_nodes;i++)
{
vertex_node temp_node=graph.vertex_array[i];
cout<<temp_node.c_value;
edge_node* temp_pointer=temp_node.first_child;
while(temp_pointer!=NULL)
{
cout<<"-->"<<graph.vertex_array[temp_pointer->index].c_value;
temp_pointer=temp_pointer->sibling;
}
cout<<endl;
}
cout<<"deep first search result:"<<endl;
dfs(graph);
cout<<endl<<"breath first search result:"<<endl;
bfs(graph);
delete_graph(graph);
return 0;
}