邻接表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 图的建 ...
随机推荐
- 一致性哈希算法(consistent hashing)【转】
一致性哈希算法 来自:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希 ...
- 小兔伴伴家庭动物园AR智能早教产品上市
2016年6月,经过乐卓大家庭所有人的共同努力,公司旗下首款新品——小兔伴伴之<家庭动物园>3D智能学习卡正式面世. 每个孩子都应该在合适的时间去体验丰富的声音.色彩和动作,<家庭动 ...
- Android加载SO库UnsatisfiedLinkError错误的原因及解决方案
Android 应用开发者应该对 UnsatisfiedLinkError 这种类型的错误比较熟悉了,这个问题一直困扰着广大的开发者,那么有没有想过有可能你什么都没做错,也会出现这个问题呢? 我们在 ...
- 用 .NET Reflector 8 查看 System.Windows.Controls 命名空间下的类
为了学习自定义控件,就想看看WPF基本元素的代码.使用到工具.NET Reflector. System.Windows.Controls 命名空间在PresentationFramework.dll ...
- 【解决方案】HTTP could not register URL http://+:6001/
Failed to host the DeployerNotificationReceiverSystem.ServiceModel.AddressAccessDeniedException: HTT ...
- android 视频的缩略图 缓存机制和 异步加载缩略图
在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确实是这样的,百度一下,每个帖子都知道制作视频缩略图的方法,在这里确实也是一样的,但是我要 ...
- gcview使用
1.下载适用的版本 https://github.com/chewiebug/GCViewer Supported verbose:gc formats are: Oracle JDK 1.8 -Xl ...
- gulp-notify处理报错----gulp系列(二)
上一节,以less为例,入门了gulp,并为任务结构做了抽离. 前端们,gulp该用起来了,简单的demo入门——gulp系列(一) 本节学习下gulp-notify,官方这样解释的: gulp-no ...
- Windows7 x64 系统下安装 Nodejs 并在 WebStorm 9.0.1 下搭建编译 LESS 环境
1. 打开Nodejs官网http://www.nodejs.org/,点“DOWNLOADS”,点64-bit下载“node-v0.10.33-x64.msi”. 2. 下载好后,双击“node-v ...
- .NET ORM 哪家强
ORM到底哪家强? 很多人都想知道这个问题,自已也没测试过,只能道听途说. 闲的无聊就将几个ORM拿出来比一比,假如怀疑测试代码有问题可以将它下载下来慢慢研究. 参赛ORM 1.SqlSugar:是一 ...