⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 我的.cpp

📁 K-MEANS算法: k-means 算法接受输入量 k ;然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高;而不同聚类中的对象相似度较小。聚类相似度是利用各
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#define MAX_VEXTEX_NUM 20
#define M 20
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0

typedef int ElemType;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;

typedef struct VNode
{
int data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VEXTEX_NUM];

typedef struct
{
AdjList vertices;
int vexnum, arcnum;
}ALGraph;

typedef struct 
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;

void InitStack(SqStack *);               
int Pop(SqStack *, ElemType *);
void Push(SqStack *,ElemType );
int StackEmpty(SqStack *);
void CreatGraph(ALGraph *);
void FindInDegree(ALGraph , int * );
void TopologicalSort(ALGraph );


void InitStack(SqStack *S)
{
S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S->base)
{
printf("memory allocation failed, goodbye");
exit(1);
}
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}

int Pop(SqStack *S,ElemType *e)
{
if(S->top==S->base)
{
return ERROR;
}
*e=*S->top;//改
S->top--;
return *e;//改
}

void Push(SqStack *S,ElemType e)
{
if(S->top-S->base>=S->stacksize)
{
S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base)
{
printf("memory allocation failed, goodbye");
exit(1);
}
S->top = S->base+S->stacksize-1;//改
S->stacksize+=STACKINCREMENT;
}
*S->top++;//改
*S->top=e;

}

int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return OK;
else
return ERROR;
}

void CreatGraph(ALGraph *G)
{
int m, n, i;
ArcNode *p;

printf("请输入顶点数和边数:");
scanf("%d%d",&G->vexnum,&G->arcnum);

for (i = 1; i <= G->vexnum; i++)
{
G->vertices[i].data = i;
G->vertices[i].firstarc = NULL;
}

for (i = 1; i <= G->arcnum; i++)       
{
printf("\n请输入存在边的两个顶点的序号:");
scanf("%d%d",&n,&m);
while (n < 0 || n > G->vexnum || m < 0 || m > G->vexnum)
{
printf("输入的顶点序号不正确 请重新输入:");
scanf("%d%d",&n,&m);
}

p = (ArcNode*)malloc(sizeof(ArcNode));
if (p == NULL)
{
printf("memory allocation failed,goodbey");
exit(1);
}
p->adjvex = m;
p->nextarc = G->vertices[n].firstarc;
G->vertices[n].firstarc = p;
}

printf("建立的邻接表为:\n");          
for(i = 1; i <= G->vexnum; i++)
{
printf("%d",G->vertices[i].data);
for(p = G->vertices[i].firstarc; p; p = p->nextarc)
printf("%3d",p->adjvex);
printf("\n");
}


}

void FindInDegree(ALGraph G, int indegree[])
{
int i;
ArcNode *p = NULL;

for (i = 1; i <= G.vexnum; i++)
{
indegree[i] = 0;
}

for (i = 1; i <= G.vexnum; i++)
{
p = G.vertices[i].firstarc;
while (p)
{
indegree[p->adjvex]++;
p = p->nextarc;
}
}
}

void TopologicalSort(ALGraph G)       
{
int indegree[M];
int i, k, n;
int count = 0;
ArcNode *p;
SqStack S;

FindInDegree(G, indegree);
InitStack(&S);

for (i = 1; i <= G.vexnum; i++)
{
printf("第%d个点的入度为%d \n", i, indegree[i]);
}
printf("\n");
for ( i = 1; i <= G.vexnum; i++)
{
if (!indegree[i])
Push(&S,i);
}


printf("进行拓扑排序输出顺序为:");         
while(!StackEmpty(&S))
{
Pop(&S,&n);
printf("%4d",G.vertices[n].data);
count++;
for (p = G.vertices[n].firstarc;  p != NULL;  p = p->nextarc)
{
k = p->adjvex;
if (!(--indegree[k]))
{
Push(&S,k);
}
}

}
printf("\n");
if (count < G.vexnum)
{
printf("出现错误\n");
}
else
{
printf("排序成功\n");
}
}
int main(void)            
{
ALGraph G;

CreatGraph(&G);
TopologicalSort(G);

system("pause");
return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -