Experimental data structure, Report III

1. Enter the specified number of edges and the number of vertices FIG establishment, and outputs the result of depth-first traversal and breadth-first traversal.

1) Problem Description: A simple design in the main program menu, respectively calling the corresponding function feature:

The establishment of a ... map

FIG depth-first traversal ... 2

3 ... breadth-first graph traversal

0 ... end

 

2) Experimental requirements: The following function is defined in the program, and to realize the function of the functional requirements:

   CreateGraph (): established by data from the keyboard of FIG.

   DFSGrahp (): depth-first traversal FIG.

   BFSGrahp (): breadth-first graph traversal

 

3) experiments suggest:

FIG  table memory can be contiguous or adjacent matrix;

 FIG stored data type definition (adjacent table storage)  

# Define MAX_VERTEX_NUM 8 // maximum number of vertices    

typedef struct ArcNode

           {  int  adjvex; 

              struct ArcNode *nextarc;

              int weight; // right side

           } ArcNode; // table node       

 # Define VertexType int // vertex element type

   typedef struct VNode

            {Int degree, indegree; // degrees vertex, the degree of               

VertexType data;

               ArcNode  *firstarc;

} Vnode / * first node * /, AdjList [MAX_VERTEX_NUM];       

typedef struct{          

AdjList vertices;

               int vexnum, arcnum; // actual actual number of vertices, edges

        }ALGraph; 

4) attention to the problem:

Note that the storage structure  appreciated that when each of the algorithm used.

 Note the difference between positive and inverse adjacency.

#include<iostream>
using namespace std;
#include<cstdlib>
#include<queue>             //c++的队列库函数 
#include<algorithm>
# define MAX_VERTEX_NUM 8   //顶点最大个数  
typedef struct ArcNode{  
    int  adjvex;	
    ArcNode *nextarc;
}ArcNode;                        
typedef struct VNode{                
    int  data;             //int 类型 
    ArcNode  *firstarc;
}Vnode, AdjList[MAX_VERTEX_NUM];        
typedef struct{           
    AdjList vertex;
    int vexnum,arcnum;     //顶点的实际数,边的实际数
}Graph;  

void CreateGraph(Graph &G){
	ArcNode* s;
	int i,j;
	int k,d;
	int n = G.vexnum;
	int e = G.arcnum; 
  	for(i=1; i<=n; i++){     //n为顶点数、e为边数 
		G.vertex[i].data = i;
		G.vertex[i].firstarc = NULL;
	}
	for(i=1; i<=e; i++){
            // 输入边
            scanf("%d%d",&k,&d);
			s = (ArcNode*)malloc(sizeof(ArcNode));
			s->adjvex = d;
			s->nextarc = G.vertex[k].firstarc;
			G.vertex[k].firstarc = s;
			s = (ArcNode*)malloc(sizeof(ArcNode));
			s->adjvex = k;
			s->nextarc = G.vertex[d].firstarc;
			G.vertex[d].firstarc = s;
	}
	for(i=1;i<=n;i++){
		int count=0;
		int paixu[100] = {0};
		ArcNode* r;
		r = (ArcNode*)malloc(sizeof(ArcNode));
		r = G.vertex[i].firstarc;
		while(r!=NULL){
			paixu[count] = r->adjvex;
			count++;
			r = r->nextarc;
		}
		sort(paixu,paixu+100);
		
		r = G.vertex[i].firstarc;
		count =100-count;
		while(r!=NULL){
		    r->adjvex = paixu[count];
			count++;
			r = r->nextarc;
		}
	}
} 
int flag[100] = {0};
int con=1;
void DFS(Graph G,int v){
	if(con < G.vexnum){
	cout<<G.vertex[v].data<<" "; 
	con++;		
	}
	else{
		cout<<G.vertex[v].data;
	}
	ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
	p = G.vertex[v].firstarc; 
    flag[v] = 1;
    while(p){
	    if(!flag[p->adjvex]){
	        DFS(G,p->adjvex);	
		}
    	p = p->nextarc;			
	}
}
int flag1[100] = {0};
int con1 = 1;
void BFS(Graph G,int v){
	queue <int> Q;
	cout<<G.vertex[v].data<<" "; 
	con1++;
	ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
    flag1[v] = 1;
    Q.push(v);
    while(!Q.empty()){
    	v = Q.front();
    	Q.pop();
    	p = G.vertex[v].firstarc;
    	while(p){
    		if(!flag1[p->adjvex]){
    			if(con1 < G.vexnum){
				    cout<<G.vertex[p->adjvex].data<<" ";
					con1++; 	
				} 
				else{
					cout<<G.vertex[p->adjvex].data;
				} 
    			flag1[p->adjvex] = 1;
    			Q.push(p->adjvex);
			}
			p = p->nextarc;
		}
	}
}
int main(){
	int T;
	Graph G;
	cout<<"1...图的建立"<<endl;
	cout<<"2...深度优先遍历图"<<endl;
	cout<<"3...广度优先遍历图"<<endl;
	cout<<"0...结束"<<endl;
	cout<<endl;
	scanf("%d",&T);
	while(T != 0){
		if(T == 1){
		    cout<<"请输入图的顶点数和边数"<<endl;
            scanf("%d%d",&G.vexnum,&G.arcnum);   //  顶点数 边数 
		    CreateGraph(G);   //建立图	
		    cout<<"建图成功"<<endl; 
		}
		else if(T == 2){
			cout<<"深度优先遍历结果为:"<<endl;
			DFS(G,1);     //深度优先遍历图 
			cout<<endl;
		}
		else if(T == 3){
			cout<<"广度优先遍历结果为:"<<endl;
			BFS(G,1);     //广度优先遍历图 
            cout<<endl; 
		}
		scanf("%d",&T);
	}
	return 0;
}

2. Teaching planning issues

1) Problem Description: Software professional students to study a range of courses, some courses must be in order to learn after its completion Prerequisite.

2) Experimental requirements: Suppose learning time for each course of a semester, students design the test for the teaching program, so that they can complete a full course of professional requirements in the shortest time.

3) to achieve prompt:

  • Vertex on behalf of courses, courses on behalf of the arc has to repair relations, press relations program has established directed acyclic graph.
  • Topological sort implementation.
#include<iostream>
using namespace std;
#include<cstdlib>
#include<stack>             //c++的栈库函数 
#include<algorithm>
# define MAX_VERTEX_NUM 8   //顶点最大个数  
typedef struct ArcNode{  
    int  adjvex;	
    ArcNode *nextarc;
}ArcNode;                  //表结点        
#define VertexType  int    //顶点元素类型
typedef struct VNode{  
    int  degree,indegree;  //顶点的度,入度                
    int  data;             //int 类型 
    ArcNode  *firstarc;
}Vnode, AdjList[MAX_VERTEX_NUM];        
typedef struct{           
    AdjList vertex;
    int vexnum,arcnum;     //顶点的实际数,边的实际数
}Graph;  

void CreateGraph(Graph &G){
	ArcNode* s;
	int i,j;
	int k,d;
	int n = G.vexnum;
	int e = G.arcnum; 
  	for(i=1; i<=n; i++){     //n为顶点数、e为边数 
		G.vertex[i].data = i;
		G.vertex[i].firstarc = NULL;
		G.vertex[i].indegree = 0;
	}
	cout<<"请输入各边"<<endl;
	for(i=1; i<=e; i++){
            scanf("%d%d",&k,&d);
			s = (ArcNode*)malloc(sizeof(ArcNode));
			s->adjvex = d;
			G.vertex[d].indegree++; 
			s->nextarc = G.vertex[k].firstarc;
			G.vertex[k].firstarc = s;
	}
	for(i=1;i<=n;i++){
		int count=0;
		int paixu[100] = {0};
		ArcNode* r;
		r = (ArcNode*)malloc(sizeof(ArcNode));
		r = G.vertex[i].firstarc;
		while(r!=NULL){
			paixu[count] = r->adjvex;
			count++;
			r = r->nextarc;
		}
		sort(paixu,paixu+100);
		
		r = G.vertex[i].firstarc;
		count =100-count;
		while(r!=NULL){
		    r->adjvex = paixu[count];
			count++;
			r = r->nextarc;
		}
	}
} 
int TS(Graph G){
	int i,k,tmp,count=1;
	stack<int> s;
	

	ArcNode *p;
	for(i=G.vexnum;i>0;i--){
		if( G.vertex[i].indegree == 0){
			s.push(i);
		}
	} 
	cout<<endl<<"结果为:"<<endl;
	while(!s.empty()){
	    int tm[100]={0};	
		int con=0;
		while(!s.empty()){
			tm[con] = s.top();
			con++;
			s.pop();
		}
		sort(tm,tm+100);
		int con1 = con;
		while(con){
			s.push(tm[100-con1+con-1]);
			con--;
		}
		tmp = s.top();
		s.pop();
		if(count<G.vexnum){
		    cout<<G.vertex[tmp].data<<" ";	
		    count++;
		}
		else{
			cout<<G.vertex[tmp].data;
		}
		for(p = G.vertex[tmp].firstarc; p; p=p->nextarc){
			k = p->adjvex;
			if(!(--G.vertex[k].indegree)){
				s.push(k);
			}
		}
		
	}
}

int main(){
	Graph G;
	cout<<"请输入顶点数和边数:"<<endl; 
	scanf("%d%d",&G.vexnum,&G.arcnum);   //  顶点数 边数 
	CreateGraph(G);   //建立图	
	TS(G);
	return 0;
}

3. Given the actual background to solve the shortest path problem

1) Description of the problem: Assuming that a weighted network indicating bus lines to a region, and FIG. Vertices representative of an important place in some regions, has an arc representative of the bus lines, arcs on the right in the line fare (or take the time required), try to design a traffic guidance system, to guide those who come to consult with the lowest fare or the least amount of time to reach the other places in the area from a place.

2) Experimental requirements: use Dijkstra algorithm for finding the lowest fare

3) to achieve prompt:

  • This problem can be reduced to a request to have a problem with the right in FIG shortest path between the vertices.
  • Tickets for the right to establish a directed graph, then the shortest path using Dijkstra's algorithm and the path length.
#include<iostream>
using namespace std;
typedef struct Arc{
	int quan;            
}AdjMatrix[500][500];    //矩阵

typedef struct Graph{
	int vertex[1000];
	AdjMatrix arcs;
	int vexnum,arcnum;
}; 
int main(){
	int n,m;
	Graph G;
	cout<<"请输入顶点数和边数:"<<endl;
	cin>>n>>m;
	int i,j;
	for(i=1;i<=n;i++){         //初始化矩阵 
		for(j=1;j<=n;j++){
			G.arcs[i][j].quan = 99999;
		}
	}
	int m1 = m;
	int a,b,c;
	int P;
	cout<<"请输入边信息:"<<endl; 
	while(m1--){              //赋权值 
		cin>>a>>b>>c;
		G.arcs[a][b].quan = c;
	}
	int q[1000]; 
	for(i=0;i<=1000;i++){
		q[i] = 99999;
	}
	int n1 = n-1;
	int nowi = 1, nowq = 0,nowj = 1;
	int z = 9999;
	while(n1--){
		int min = 99999;
		for(j=1;j<=n;j++){
			if(nowi != j){         
			    if(G.arcs[nowi][j].quan < q[j]){
			    	q[j] = G.arcs[nowi][j].quan;
			    	if(nowj == j){
			    		q[j] += nowq;
					}	
				} 
				if(G.arcs[nowi][j].quan < min){
					min = G.arcs[nowi][j].quan;
				}
			}
		}
		for(j=1;j<=n;j++){    
			if(min == q[j]){
				nowj = j;
			}
		}	
		nowq = nowq + min;
		q[nowj] = nowq;
		int min1 = 99999;
		for(i=1;i<=n;i++){
			if(q[i]<=min1){
				min1 = q[i];
				nowi = i;
			}
	    }
		nowq = min1;
        if(nowi==n && nowq<z){
            z = nowq;       	
		}
		for(j=1;j<=n;j++){     
			if(min1 == q[j]){
				q[j] = 99999;
			}
		}	
	}
	cout<<"第1个节点到第n个节点的最短路径:"<<endl;
	cout<<z;
	return 0;
}

4. The total cost of using the minimum spanning tree algorithm to solve the problem of communication network lowest

1) Description of the problem: If n build a communication network between cities, set up to n-1 lines. How the lowest economic cost of building the communications network is a network of the minimum spanning tree problem.

2) Experimental requirements: Prim minimum spanning tree algorithm for using the network.

3) to achieve prompt: communication lines once established, must be bi-directional. Therefore, the minimum spanning tree of the network must be free to the net. For simplicity, FIG number of vertices does not exceed 10, at the right side of the net value is set to less than 100.

#include<iostream>
using namespace std;
typedef struct Arc{
	int quan;            
}AdjMatrix[500][500];    //矩阵

typedef struct Graph{
	AdjMatrix arcs;
	int vexnum,arcnum;
}; 
int main(){
	int n,m;
	Graph G;
	cout<<"请输入顶点数和边数"<<endl;
	cin>>n>>m;
	int i,j;
	for(i=1;i<=n;i++){         //初始化矩阵 
		for(j=1;j<=n;j++){
			G.arcs[i][j].quan = 99999;
		}
	}
	int m1 = m;
	int a,b,c;
	int P;
	cout<<"请输入各边信息"<<endl;
	while(m1--){              //赋权值 
		cin>>a>>b>>c;
		G.arcs[a][b].quan = c;
		G.arcs[b][a].quan = c;
	}
	int u[1000] = {0};
	int v[1000] = {0};
	for(i=2;i<=n;i++){
		v[i] = i;
	}
	u[1] = 1;
	int n1 = n-1;
	int nowi,nowj,sum=0;
	while(n1--){
	    i=1;
		int min = 99999;
		while(i != n+1){
			if(u[i]!=0){
				j=1;
			    while(j != n+1){
			    if(G.arcs[u[i]][v[j]].quan<=min && v[j]!=0){
			    	min = G.arcs[u[i]][v[j]].quan;
			    	nowi = u[i];
			    	nowj = v[j];
				}
				j++;
		        }	
			}
		    i++;
	    }
	    G.arcs[nowi][nowj].quan = 99999;
	    G.arcs[nowj][nowi].quan = 99999;
	    sum += min; 
        u[nowj] = nowj;
		v[nowj]	= 0;
	}
	cout<<"最小代价为:"<<endl;
	cout<<sum;
	return  0;
}

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/94718238