邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
graph.c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> #include "aqueue.h" #define MAX_NUM 100
typedef char node_type; typedef struct arc_node
{
int pos;
int distance;
struct arc_node * next;
} Arc_node;//保存Node节点的相邻节点信息 typedef struct node
{
node_type info;
Arc_node * next;
} Node;//保存节点信息 typedef struct graph
{
Node adjlist[MAX_NUM];
int vertexs, brim;
} Graph;//邻接表 static Arc_node * make_node(const int pos, const int distance)
{
Arc_node * new_node = (Arc_node *)malloc( sizeof(Arc_node) );
if ( new_node == NULL )
exit(); new_node->next = NULL;
new_node->distance = distance;
new_node->pos = pos; return new_node;
} void g_create(Graph * graph)
{
int num;
int i, j, k;
char c;
Arc_node * tmp; printf("输入节点个数:");
scanf("%d", &graph->vertexs);
getchar();
printf("输入顶点信息:");
for ( i = ; i < graph->vertexs; i++ )
{
scanf("%c", &graph->adjlist[i].info);
graph->adjlist[i].next = NULL;
getchar();
}
graph->brim = ; for ( i = ; i < graph->vertexs; i++ )
{
printf("输入与节点%c相邻的节点和权值,#号键结束\n", graph->adjlist[i].info);
for ( j = ; j < graph->vertexs; j++ )
{
scanf("%c", &c);
if ( c == '#' )
{
getchar();
break;
}
scanf("%d", &num);
getchar();
for ( k = ; k < graph->vertexs; k++ )
{
if ( graph->adjlist[k].info != c )
continue;
if ( graph->adjlist[k].next == NULL )
graph->adjlist[k].next = make_node(i, num);
else
{
tmp = graph->adjlist[k].next;
while ( tmp->next != NULL )
tmp = tmp->next;
tmp->next = make_node(i, num);
}
graph->brim++;
}
}
}
graph->brim /= ;
} static void dfs_graph(Graph * graph, bool visited[], const int i);
void g_depth_first_search(Graph * graph)
{
bool visited[graph->vertexs];
int i; for ( i = ; i < graph->vertexs; i++ )
visited[i] = false; visited[] = true;
dfs_graph(graph, visited, );
} static void dfs_graph(Graph * graph, bool visited[], const int i)
{
Arc_node * tmp;
printf("%c\t", graph->adjlist[i].info); tmp = graph->adjlist[i].next;
while ( tmp != NULL )
{
if ( !visited[tmp->pos] )
{
visited[tmp->pos] = true;
dfs_graph(graph, visited, tmp->pos);
}
tmp = tmp->next;
}
} void g_breadth_first_search(Graph * graph)
{
Queue queue;
bool visited[graph->vertexs];
int pos;
int i;
Arc_node * tmp; q_init(&queue);
for ( i = ; i < graph->vertexs; i++ )
visited[i] = false; visited[] = true;
q_push(&queue, );
while ( !q_empty(&queue) )
{
pos = q_front(&queue);
printf("%c\t", graph->adjlist[pos].info);
tmp = graph->adjlist[pos].next;
while ( tmp != NULL )
{
if ( !visited[tmp->pos] )
{
visited[tmp->pos] = true;
q_push(&queue, tmp->pos);
}
tmp = tmp->next;
}
q_pop(&queue);
}
printf("\n");
} static void init_prim(Graph * graph, Graph * prim_tree);
void g_prim(Graph * graph, Graph * prim_tree)
{
bool visited[graph->vertexs];
int i, j, k;
int power, pos;
Arc_node * tmp; for ( i = ; i < graph->vertexs; i++ )
visited[i] = false;
init_prim(graph, prim_tree); visited[] = true;
for ( i = ; i < graph->vertexs; i++ )
{
power = INT_MAX;//limits.h
for ( j = ; j < graph->vertexs; j++ )
{
if ( visited[j] )
{
tmp = graph->adjlist[j].next;
while ( tmp != NULL )
{
if ( power > tmp->distance && !visited[tmp->pos] )
{
power = tmp->distance;
pos = tmp->pos;
k = j;
}
tmp = tmp->next;
}
}
}
if ( !visited[pos] )
{
if ( prim_tree->adjlist[k].next == NULL )
{
prim_tree->adjlist[k].next = make_node(pos, power);
}
else
{
tmp = prim_tree->adjlist[k].next;
while ( tmp->next != NULL )
tmp = tmp->next;
tmp->next = make_node(pos, power);
}
visited[pos] = true;
}
}
} static void init_prim(Graph * graph, Graph * prim_tree)
{
int i; for ( i = ; i < graph->vertexs; i++ )
{
prim_tree->adjlist[i].info = graph->adjlist[i].info;
prim_tree->adjlist[i].next = NULL;
}
prim_tree->vertexs = graph->vertexs;
prim_tree->brim = graph->brim;
} //kruskal
typedef struct
{
int head;
int tail;
int power;
} Edge;
static void init_kruskal(Graph * graph, Graph * kruskal_tree);
static void my_sort(Edge * arr, int size);
void kruskal(Graph * graph, Graph * kruskal_tree)
{
int visited[graph->vertexs];
int i, j;
Edge edge[graph->brim];
int v1, v2, vs1, vs2;
Arc_node * cur, * tmp; for ( i = ; i < graph->vertexs; i++ )
visited[i] = i; for ( i = , j = ; i < graph->vertexs; i++ )
{
cur = graph->adjlist[i].next;
while ( cur != NULL )
{
if ( cur->pos > i )
{
edge[j].head = i;
edge[j].tail = cur->pos;
edge[j].power = cur->distance;
j++;
}
cur = cur->next;
}
} init_kruskal(graph, kruskal_tree);
my_sort(edge, graph->brim); for ( i = ; i < graph->brim; i += )
{
v1 = edge[i].head;
v2 = edge[i].tail;
vs1 = visited[v1];
vs2 = visited[v2];
if ( vs1 != vs2 )
{
if ( kruskal_tree->adjlist[v1].next == NULL )
{
kruskal_tree->adjlist[v1].next = make_node(v2, edge[i].power);
}
else
{
tmp = kruskal_tree->adjlist[v1].next;
while ( tmp->next != NULL )
tmp = tmp->next;
tmp->next = make_node(v2, edge[i].power);
}
for ( j = ; j < graph->vertexs; j++ )
{
if ( visited[j] == vs2 )
visited[j] = vs1;
}
}
}
} static void init_kruskal(Graph * graph, Graph * kruskal_tree)
{
int i; kruskal_tree->vertexs = graph->vertexs;
kruskal_tree->brim = graph->brim; for ( i = ; i < graph->vertexs; i++ )
{
kruskal_tree->adjlist[i].info = graph->adjlist[i].info;
kruskal_tree->adjlist[i].next = NULL;
}
} static void my_sort(Edge * arr, int size)
{
int i, j;
Edge tmp; for ( i = ; i < size - ; i++ )
{
for ( j = i + ; j < size; j++ )
{
if ( arr[i].power > arr[j].power )
{
tmp.head = arr[i].head;
tmp.tail = arr[i].tail;
tmp.power = arr[i].power; arr[i].head = arr[j].head;
arr[i].tail = arr[j].tail;
arr[i].power = arr[j].power; arr[j].head = tmp.head;
arr[j].tail = tmp.tail;
arr[j].power = tmp.power;
}
}
}
} int main(void)
{
Graph graph;
Graph prim_tree;
Graph kruskal_tree;
Arc_node * node;
int i; g_create(&graph); printf("brim = %d\n", graph.brim);
for ( i = ; i < graph.vertexs; i++ )
{
printf("%c\t", graph.adjlist[i].info);
node = graph.adjlist[i].next;
while ( node != NULL )
{
printf("%d %d\t", node->distance, node->pos);
node = node->next;
}
printf("\n");
}
printf("\n"); // g_depth_first_search(&graph);
// printf("\n");
// g_breadth_first_search(&graph); kruskal(&graph, &kruskal_tree);
printf("brim = %d\n", kruskal_tree.brim);
for ( i = ; i < kruskal_tree.vertexs; i++ )
{
printf("%c\t", kruskal_tree.adjlist[i].info);
node = kruskal_tree.adjlist[i].next;
while ( node != NULL )
{
printf("%d %d\t", node->distance, node->pos);
node = node->next;
}
printf("\n");
}
printf("\n"); return ;
}
aqueue.h
#ifndef _QUEUE_H
#define _QUEUE_H #define MAXSIZE 10 typedef struct queue
{
int * arr;
int front;
int rear;
} Queue; void q_init(Queue * queue);//初始化
void q_push(Queue * queue, const int data);//入队
void q_pop(Queue * queue);//出队
bool q_empty(Queue * queue);//为空
bool q_full(Queue * queue);//为满
int q_size(Queue * queue);//队大小
int q_front(Queue * queue);//队头元素
int q_back(Queue * queue);//队尾元素
void q_destroy(Queue * queue);//销毁 #endif //_QUEUE_h
aqueue.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h> #include "aqueue.h" void q_init(Queue * queue)
{
queue->arr = (int *)malloc( sizeof(int) * MAXSIZE );//初始化数组
assert(queue->arr != NULL);
queue->front = ;
queue->rear = ;
} void q_push(Queue * queue, const int data)
{
if ( q_full(queue) )
return;
queue->arr[queue->rear++] = data;//入队,队尾+1
queue->rear = queue->rear % MAXSIZE;//如果队尾
} void q_pop(Queue * queue)
{
if ( q_empty(queue) )
return;
queue->front = ++queue->front % MAXSIZE;//front+1,对MAXSIZE取余
} bool q_empty(Queue * queue)
{
return queue->front == queue->rear;
} bool q_full(Queue * queue)
{
return queue->front == (queue->rear + ) % MAXSIZE;
} int q_size(Queue * queue)
{
return (queue->rear - queue->front) % MAXSIZE;
} int q_front(Queue * queue)
{
assert( !q_empty(queue) );
return queue->arr[queue->front];
} int q_back(Queue * queue)
{
assert( !q_empty(queue) );
return queue->arr[queue->rear - ];
} void q_destroy(Queue * queue)
{
free(queue->arr);
}
邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)的更多相关文章
- 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)
matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
学习了图的深度优先和广度优先遍历,发现不管是教材还是网上,大都为C语言函数式实现,为了加深理解,我以C++面向对象的方式把图的深度优先和广度优先遍历重写了一遍. 废话不多说,直接上代码: #inclu ...
- 存储结构与邻接矩阵,深度优先和广度优先遍历及Java实现
如果看完本篇博客任有不明白的地方,可以去看一下<大话数据结构>的7.4以及7.5,讲得比较易懂,不过是用C实现 下面内容来自segmentfault 存储结构 要存储一个图,我们知道图既有 ...
- [源码解析] PyTorch 如何实现后向传播 (4)---- 具体算法
[源码解析] PyTorch 如何实现后向传播 (4)---- 具体算法 目录 [源码解析] PyTorch 如何实现后向传播 (4)---- 具体算法 0x00 摘要 0x01 工作线程主体 1.1 ...
- 老李推荐:第14章9节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-遍历控件树查找控件
老李推荐:第14章9节<MonkeyRunner源码剖析> HierarchyViewer实现原理-遍历控件树查找控件 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员 ...
- 图的理解:深度优先和广度优先遍历及其 Java 实现
遍历 图的遍历,所谓遍历,即是对结点的访问.一个图有那么多个结点,如何遍历这些结点,需要特定策略,一般有两种访问策略: 深度优先遍历 广度优先遍历 深度优先 深度优先遍历,从初始访问结点出发,我们知道 ...
- JavaScript实现树深度优先和广度优先遍历搜索
1.前置条件 我们提前构建一棵树,类型为 Tree ,其节点类型为 Note.这里我们不进行过多的实现,简单描述下 Note 的结构: class Node{ constructor(data){ t ...
- 顺序线性表 ---- ArrayList 源码解析及实现原理分析
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7738888.html ------------------------------------ ...
- 图的建立(邻接矩阵)+深度优先遍历+广度优先遍历+Prim算法构造最小生成树(Java语言描述)
主要参考资料:数据结构(C语言版)严蔚敏 ,http://blog.chinaunix.net/uid-25324849-id-2182922.html 代码测试通过. package 图的建 ...
随机推荐
- CentOS 7.0系统安装配置LAMP服务器(Apache+PHP+MariaDB)
CentOS 7.0接触到的用户是比较少的,今天看了站长写了一篇关于centos7中安装配置LAMP服务器的教程,下面我把文章稍加整理一下转给大家学习交流,希望例子能给各位带来帮助哦. cento ...
- MMS关键指标意义&各数值区间意义
MMS关键指标意义&各数值区间意义 What's MMS MongoDB Management Service (MMS) is a suite of services for managin ...
- Spring源码追踪2——xml解析入口
解析xml节点入口 org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDe ...
- C_中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
- 图解 & 深入浅出JavaWeb:事务必会必知
事务,大家所熟悉的事务(Transcation),基本上会就往Spring事务靠.其实Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring事务管理的基础.这篇总结 ...
- Second Day learning English
Today I have set my Microsoft word program, use it send documents to the blog site.
- VMware三个版本workstation、server、esxi的区别
VMware三个版本 workstation: 单机级,用在个人桌面系统中,需要操作系统支持 servier:工作组级,用于服务器,需要操作系统支持 esxi:企业级,用于服务器,不需要操作系统支持 ...
- svn diff excel
https://github.com/solq360/compareExcel jdk 1.7 可自己编译1.6 每个sheet第一行不允许为空 SVN对比excel差异 适合策划.财务职业使用 sv ...
- 未能找到类型或命名空间名称“Coco”(是否缺少 using 指令或程序集引用)
未能找到类型或命名空间名称"Coco"(是否缺少 using 指令或程序集引用),如果你确实引用了,那说明你引用的和你的项目环境版本不一样,.NET framework的问题,修改 ...
- Rails4 中 因为secret key 引起在production环境下无法运行
错误信息 Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml` ...