邻接表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 图的建 ...
随机推荐
- Java基础集锦——利用Collections.sort方法对list排序
要想对List进行排序,可以让实体对象实现Comparable接口,重写compareTo方法即可实现按某一属性排序,但是这种写法很单一,只能按照固定的一个属性排序,没变法变化.通过下面这种方法,可以 ...
- Solr 5.x集成中文分词word,mmseg4j
使用标准分词器,如图: 使用word分词器 下载word-1.3.jar,注意solr的版本和word分词的版本 将文件word-1.3.jar拷贝至文件夹C:\workspace\Tomcat7.0 ...
- Android Studio开发JNI工程
转:http://www.2cto.com/kf/201412/361768.html 使用Android Sutdio创建一个新的工程后,接下来记录创建NDK工程的基本步骤. 本文将达到: 1. 创 ...
- 参照nopCommerce框架开发(NextCMS)
很久没有更新博客了,现在已经不写.NET,转前端半年多了. 半年前在创业公司,做电子商务网站,用的是NopCommerce框架(3.2),这个框架还是相当不错的,经过一段时间的摸索,基本入门,于是就开 ...
- 实时流式计算框架Storm 0.9.0发布通知(中文版)
Storm0.9.0发布通知中文翻译版(2013/12/10 by 富士通邵贤军 有错误一定告诉我 shaoxianjun@hotmail.com^_^) 我们很高兴宣布Storm 0.9.0已经成功 ...
- C#的回调被C++调用
__stdcall 要加这个 extern "C" __declspec(dllexport) 要用这样的东东 定义 extern "C" __declspec ...
- WEB安全入门(转)
一. 首先你得了解Web Web分为好几层,一图胜千言:事实是这样的:如果你不了解这些研究对象是不可能搞好安全研究的.这样看来,Web有八层(如果把浏览器也算进去,就九层啦,九阳神功……)!!!每层都 ...
- PostgreSQL和Greenplum、Npgsql
PostgreSQL和Greenplum.Npgsql 想着要不要写,两个原因“懒”和“空”.其实懒和空也是有联系的,不是因为懒的写,而是因为对PostgreSQL和Npgsql的知识了解匮乏,也就懒 ...
- bootstrap插件学习-bootstrap.carousel.js
先看bootstrap.carousel.js的结构 var Carousel = function (element, options){} //构造器 Carousel.prototype = { ...
- multi-CPU, multi-core and hyper-thread--转
原文地址:http://stackoverflow.com/questions/680684/multi-cpu-multi-core-and-hyper-thread Multi-CPU was t ...