数据结构上机——图(第一次)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;

#define MaxValue 32700
const int NumEdges = 50;         //边条数
const int NumVertices = 10;    //顶点个数
typedef char VertexData;        //顶点数据类型
typedef int EdgeData;              //边上权值类型
typedef struct {
    VertexData vexList[NumVertices];   //顶点表
    EdgeData Edge[NumVertices][NumVertices];
    //邻接矩阵, 可视为边之间的关系
    int n, e;    //图中当前的顶点个数与边数
} MTGraph;

int visited[100];
queue<int>Q;
void BFS(MTGraph G,int v) {
    Q.push(v);
    int t;
    while(!Q.empty()) {
        t=Q.front();
        Q.pop();
        visited[t]=1;
        cout<<t<<" ";
        for(int i=1; i<=G.n; i++) {
            if( (!visited[i]) && G.Edge[t][i]<MaxValue) Q.push(i);
        }
    }

}

int main() {
    MTGraph G;
    int s,t,path;
    cout<<"请输入顶点个数"<<endl;
    cin>>G.n;
    cout<<"请输入边数"<<endl;
    cin>>G.e;

    for(int i=1; i<=G.n; i++) {
        for(int j=1; j<=G.n; j++) {
            G.Edge[i][j]=MaxValue;
        }
    }
    cout<<"请输入各边权值"<<endl;
    for(int i=1; i<=G.e; i++) {
        cin>>s>>t>>path;
        G.Edge[s][t]=path;
    }

    for(int i=1; i<=G.n; i++) {
        visited[i]=0;
    }
    BFS(G,1);
    cout<<endl;
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;

#define MAX_VERTEX_NUM 20
const int NumEdges = 50;         //边条数
const int NumVertices = 10;    //顶点个数

typedef char VertexData;        //顶点数据类型
typedef int EdgeData;              //边上权值类型

typedef struct node {          //边结点
    int dest;                            //目标顶点下标
    EdgeData cost;           //边上的权值
    struct node * link;           //下一边链接指针
} EdgeNode;

typedef struct {                   //顶点结点
    VertexData data;              //顶点数据域
    EdgeNode * firstAdj;       //边链表头指针
} VertexNode;

typedef struct {                   //图的邻接表
    VertexNode VexList [NumVertices]; //邻接表
    int n, e;            //图中当前的顶点个数与边数
} AdjGraph;

void CreateGraph (AdjGraph G) {
    int tail,head,weight;
    cout<<"请输入顶点个数和边数"<<endl;
    scanf ("%d %d", &G.n, &G.e);
    //输入顶点个数和边数
    cout<<"请输入顶点信息"<<endl;
    for ( int i = 0; i < G.n; i++) {
        scanf("%c",&G.VexList[i].data ); //输入顶点信息
        G.VexList[i].firstAdj = NULL;
    }
    cout<<"请输入边"<<endl;
    for ( int i = 0; i < G.e; i++) {         //逐条边输入
        scanf ("%d %d %d",&tail,&head,&weight );
        EdgeNode * p ;
        p->dest = head;
        p->cost = weight;
        //链入第 tail 号链表的前端
        p->link = G.VexList[tail].firstAdj;
        G.VexList[tail].firstAdj = p;
        p = new EdgeNode;
        p->dest = tail;
        p->cost = weight;
        //链入第 head 号链表的前端
        p->link = G.VexList[head].firstAdj;
        G.VexList[head].firstAdj = p;
    }
}

int main() {
    AdjGraph G;
    CreateGraph ( G );

    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib> 
using namespace std;

#define MaxValue 32700
const int NumEdges = 50;         //边条数
const int NumVertices = 10;    //顶点个数
typedef char VertexData;        //顶点数据类型 
typedef int EdgeData;              //边上权值类型
typedef struct {        
    VertexData vexList[NumVertices];   //顶点表
    EdgeData Edge[NumVertices][NumVertices];
                //邻接矩阵, 可视为边之间的关系
    int n, e;    //图中当前的顶点个数与边数
} MTGraph;

int visited[100];

void DFS(MTGraph G,int v){
    visited[v]=1;
    cout<<v<<" ";
    for(int i=1;i<=G.n;i++){
        if( (!visited[i]) && G.Edge[v][i]<MaxValue) DFS(G,i);
    }
}

int main(){
    MTGraph G;
    int s,t,path;
    cout<<"请输入顶点个数"<<endl;
    cin>>G.n; 
    cout<<"请输入边数"<<endl;
    cin>>G.e; 

    for(int i=1;i<=G.n;i++){
        for(int j=1;j<=G.n;j++){
            G.Edge[i][j]=MaxValue;
        }
    }
    cout<<"请输入各边权值"<<endl;
    for(int i=1;i<=G.e;i++){
        cin>>s>>t>>path;
        G.Edge[s][t]=path;
    }

    for(int i=1;i<=G.n;i++){
        visited[i]=0;
    }
    DFS(G,1);
    cout<<endl;
    return 0;
} 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;

#define MaxValue 32700
const int NumEdges = 50;         //边条数
const int NumVertices = 10;    //顶点个数
typedef char VertexData;        //顶点数据类型
typedef int EdgeData;              //边上权值类型
typedef struct {
    VertexData vexList[NumVertices];   //顶点表
    EdgeData Edge[NumVertices][NumVertices];
    //邻接矩阵, 可视为边之间的关系
    int n, e;    //图中当前的顶点个数与边数
} MTGraph;

int visited[100];


int main() {
    MTGraph G;
    int s,t,path;
    cout<<"请输入顶点个数"<<endl;
    cin>>G.n;
    cout<<"请输入边数"<<endl;
    cin>>G.e;

    for(int i=1; i<=G.n; i++) {
        for(int j=1; j<=G.n; j++) {
            if(i==j) G.Edge[i][j]=0;
            else G.Edge[i][j]=MaxValue;
        }
    }
    cout<<"请输入各边权值"<<endl;
    for(int i=1; i<=G.e; i++) {
        cin>>s>>t>>path;
        G.Edge[s][t]=G.Edge[t][s]=path;
    }

    for(int i=1; i<=G.n; i++) {
        visited[i]=0;
    }
   for(int i=1; i<=G.n; i++) {
        for(int j=1; j<=G.n; j++) {
            printf("%d\t", G.Edge[i][j]);
        }
        cout<<endl;
    }
    system("pause");
    cout<<endl;
    return 0;
}
/*
0 1 28
1 2 16
2 3 12
3 4 22
4 5 25
5 0 10
4 6 24
3 6 18
1 6 14*/
#include <stdio.h>
#include <malloc.h>
//#include "graph.h" 
#include<iostream>
using namespace std;
typedef int InfoType;
#define MAXV 100                
#define INF 32767           

typedef struct {    //邻接矩阵
    int no;                     
    InfoType info;          
} VertexType;               
typedef struct              
{   int edges[MAXV][MAXV];  
int n,e;                    
VertexType vexs[MAXV];      
} MGraph;                       

typedef struct ANode         //邻接链表     
{   int adjvex;                 
struct ANode *nextarc;      
InfoType info;           
} ArcNode;
typedef int Vertex;
typedef struct Vnode        
{   Vertex data;            
ArcNode *firstarc;      
} VNode;
typedef VNode AdjList[MAXV];
typedef struct 
{   AdjList adjlist;            
int n,e;                    
} ALGraph;                      

void MatToList(MGraph g,ALGraph *&G){   
    int i,j;
    ArcNode *p;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    for (i=0;i<g.n;i++)             
        G->adjlist[i].firstarc=NULL;
    for (i=0;i<g.n;i++)             
        for (j=g.n-1;j>=0;j--)
            if (g.edges[i][j]!=0)   
            {   p=(ArcNode *)malloc(sizeof(ArcNode));   
            p->adjvex=j;
            p->nextarc=G->adjlist[i].firstarc;  
            G->adjlist[i].firstarc=p;
            }
            G->n=g.n;G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g){   
    int i;
    ArcNode *p;
    for (i=0;i<G->n;i++)
    {   p=G->adjlist[i].firstarc;
    while (p!=NULL)
    {   g.edges[i][p->adjvex]=1;
    p=p->nextarc;
    }
    }
    g.n=G->n;g.e=G->e;
}
void DispMat(MGraph g){
    int i,j;
    for (i=0;i<g.n;i++)
    {
        for (j=0;j<g.n;j++)
            printf("%3d",g.edges[i][j]);
        printf("\n");
    }
}
void DispAdj(ALGraph *G)

{
    int i;
    ArcNode *p;
    for (i=0;i<G->n;i++)
    {
        p=G->adjlist[i].firstarc;

        cout<<i<<":";
        while (p!=NULL)
        {

            cout<<p->adjvex<<" ";
            p=p->nextarc;
        }
        printf("\n");
    }
}

int main()
{
    int i,j;
    MGraph g,g1;
    ALGraph *G;
    int m,n;
    cin>>n>>m;
    int A[n][n];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>A[i][j];
        }
    }

    g.n=n;g.e=m;
    for (i=0;i<g.n;i++)
        for (j=0;j<g.n;j++)
            g.edges[i][j]=A[i][j];


        G=(ALGraph *)malloc(sizeof(ALGraph));

        MatToList(g,G);
        DispAdj(G);

}


猜你喜欢

转载自blog.csdn.net/wcs_152/article/details/78611154