无向图的邻接矩阵
#include <iostream>
using namespace std;
typedef struct linjiejuzhen{
char a[100];
int b[100][100];
int dian,bian;
}linjie;
int find1(linjie t,char x)
{
int i;
for(i=0;i<t.dian;i++)
{
if(t.a[i]==x)
return i;
}
return -1;
}
int createju(linjie &t)
{
scanf("%d %d",&t.dian,&t.bian);
int i,j,k;
char m,n;
for(i=0;i<t.dian;i++)
{
scanf(" %c",&t.a[i]);
}
for(i=0;i<t.dian;i++)
{
for(j=0;j<t.dian;j++)
t.b[i][j]=0;
}
for(i=0;i<t.bian;i++)
{
scanf(" %c %c",&m,&n);
j=find1(t,m);
k=find1(t,n);
t.b[k][j]=1;
t.b[j][k]=1;
}
for(i=0;i<t.dian;i++)
{
for(j=0;j<t.dian;j++)
printf("%d ",t.b[i][j]);
printf("\n");
}
}
int main()
{
linjie t;
createju(t);
return 0;
}
图的邻接表
#include <iostream>
using namespace std;
typedef struct linjie{
int num;
int inf;
struct linjie *next;
}linjie;
typedef struct head{
char data;
linjie *down;
}head;
typedef struct dian{
head a[100];
int dian;
int bian;
}algraph;
int getnumber(char x,algraph t)
{
int i;
for(i=0;i<t.dian;i++)
{
if(x==t.a[i].data)
return i;
}
return -1;
}
void creategraph(algraph &t)
{
cin>>t.dian>>t.bian;
int i,j,n1,n2;
char v1,v2;
linjie *p1,*p2;
for(i=0;i<t.dian;i++)
{
cin>>t.a[i].data;
t.a[i].down=NULL;
}
for(j=0;j<t.bian;j++)
{
cin>>v1>>v2;
n1=getnumber(v1,t);
n2=getnumber(v2,t);
p1=new linjie;
p1->num=n1;
p1->next=t.a[n2].down;
t.a[n2].down=p1;
p2=new linjie;
p2->num=n2;
p2->next=t.a[n1].down;
t.a[n1].down=p2;
}
}
void printgraph(algraph t)
{
int i,j;
linjie *p;
for(i=0;i<t.dian;i++)
{
cout<<t.a[i].data ;
p=t.a[i].down;
while(p!=NULL)
{
cout<<p->num;
p=p->next;
}
printf("\n");
}
}
int visit[10]={0};
void DFS(algraph t,int v)
{
cout<<v;
int i,j;
int w;
visit[v]=1;
linjie *p;
p=t.a[v].down;
while(p!=NULL)
{
w=p->num;
if(visit[w]==0)
DFS(t,w);
p=p->next;
}
}
typedef struct Node {
int data;
struct Node *next;
}Node, *pNode;
pNode p;
void InitQueue()
{
p = (struct Node *)malloc(sizeof(struct Node));
p->next = p;
}
void EnQueue(int v)
{
pNode r,t=p;
r = (struct Node *)malloc(sizeof(struct Node));
r->data=v;
while(t->next!=p)
{
t=t->next;
}
t->next=r;
r->next=p;
}
int isEmpty()
{
if(p->next==p)
return 1;
else
return 0;
}
void DeQueue(int &v)
{
pNode t=p->next;
v=t->data;
p->next=t->next;
free(t);
}
void BFS(algraph t,int v)
{
int u,w;
linjie *r;
visit[v]=1;
InitQueue();
EnQueue(v);
while(!isEmpty())
{
DeQueue(u);
cout<<u;
r=t.a[u].down;
while(r!=NULL)
{
if(visit[r->num]==0)
{
visit[r->num]=1;
EnQueue(r->num);
}
r=r->next;
}
}
}
void degree(algraph t)
{
int c[t.dian][2]={0},i,j;
linjie *p;
for(i=0;i<t.dian;i++)
{
p=t.a[i].down;
while(p!=NULL)
{
c[i][0]++;
for(j=0;j<t.dian;j++)
if(p->num==j)
{
c[j][1]++;
}
p=p->next;
}
}
for(i=0;i<t.dian;i++)
{
cout<<i<<':'<<c[i][1]<<' '<<c[i][0]<<' '<<c[i][0]+c[i][1]<<endl;
}
}
int main()
{
algraph t;
int v;
creategraph(t);
cin>>v;
printgraph(t);
DFS(t,v);
return 0;
}
邻接矩阵转化成邻接表
#include <iostream>
#include <string.h>
using namespace std;
typedef struct linjiejuzhen{
char a[100];
int b[100][100];
int dian,bian;
}ljjz;
typedef struct linjie{
int num;
int inf;
struct linjie *next;
}linjie;
typedef struct head{
char data;
linjie *down;
}head;
typedef struct dian{
head a[100];
int dian;
int bian;
}algraph;
int getnumber(char x,algraph t)
{
int i;
for(i=0;i<t.dian;i++)
{
if(x==t.a[i].data)
return i;
}
return -1;
}
void creategraph(ljjz &p,algraph &t)
{
int i,j,n1,n2;
char v1,v2;
linjie *p1,*p2;
t.dian=p.dian;
p.bian=t.bian;
for(i=0;i<t.dian;i++)
{
t.a[i].data=p.a[i];
t.a[i].down=NULL;
}
for(i=0;i<p.dian;i++)
for(j=0;j<p.dian;j++)
{
if(p.b[i][j]==1)
{
p1=new linjie;
p1->num=j;
p1->next=t.a[i].down;
t.a[i].down=p1;
}
}
}
void printgraph(algraph t)
{
int i,j;
linjie *p;
for(i=0;i<t.dian;i++)
{
cout<<t.a[i].data ;
p=t.a[i].down;
while(p!=NULL)
{
cout<<p->num;
p=p->next;
}
printf("\n");
}
}
int main()
{
int c[5][5]={{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,0,1,0,0},{0,1,1,0,0}};
char *d[5]={"a","b","c","d","e"};
int i,j;
algraph t;
ljjz p;
p.dian=5;
p.bian=6;
for(i=0;i<p.dian;i++)
p.a[i]=*d[i];
for(i=0;i<p.dian;i++)
for(j=0;j<p.dian;j++)
p.b[i][j]=c[i][j];
creategraph(p,t);
printgraph(t);
return 0;
}