图的邻接矩阵实现(c)
参考:算法:c语言实现 一书
图的邻接矩阵实现
#ifndef GRAPH
#define GRAPH /*
图的邻接矩阵实现
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h> struct edge{
int v;
int w;
}; struct graph{
int v;
int e;
int **adj;
}; edge EDGE(int v, int w)
{
edge e;
e.v = v; e.w = w;
return e;
} int** matrixInit(int r, int c, int val)
{
int **t = (int**)malloc(r*sizeof(int*));
for (int i = 0; i < r; ++i)
t[i] = (int*)malloc(c*sizeof(int));
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
t[i][j] = val;
return t;
} graph* graphInit(int v)
{
graph* g = (graph*)malloc(sizeof(graph));
g->v = v;
g->e = 0;
g->adj = matrixInit(v, v, 0);
return g;
} void graphInsert(graph* g, edge e)
{
int v = e.v, w = e.w;
if (g->adj[v][w] == 0) ++g->e;
g->adj[v][w] = 1;
g->adj[w][v] = 1;
} void graphRemove(graph* g, edge e)
{
int v = e.v, w = e.w;
if (g->adj[v][w] == 1) --g->e;
g->adj[v][w] = 0;
g->adj[w][v] = 0;
} int graphEdge(graph* g, edge a[])
{
int e = 0;
for (int v = 0; v < g->v; ++v)
for (int w = 0; w < g->v; ++w)
if (g->adj[v][w] == 1)
a[e++] = EDGE(v, w);
return e;
} graph* graphCopy(graph* g)
{
graph *cg = graphInit(g->v);
cg->v = g->v;
cg->e = g->e;
for (int i = 0; i < g->v; ++i)
for (int j = 0; j < g->v; ++j)
(cg->adj)[i][j] = (g->adj)[i][j];
return cg;
} void graphDestroy(graph* &g)
{
for (int i = 0; i < g->v; ++i)
free((g->adj)[i]);
free(g->adj);
free(g);
} void graphShow(graph* g)
{
printf("%d vertices, %d edges\n", g->v, g->e);
for (int i = 0; i < g->v; ++i){
printf("%2d:", i);
for (int j = 0; j < g->v; ++j)
if ((g->adj)[i][j] == 1) printf("%2d", j);
printf("\n");
}
} #endif
对各个函数的简单测试:
#include"graph.h" int main()
{
printf("\tgraph of matrix test:\n");
srand(time(NULL));
graph* g = graphInit(10);
edge e[5];
for (int i = 0; i < 5; ++i){
e[i].v = rand() % 10;
e[i].w = rand() % 10;
graphInsert(g, e[i]);
}
graphShow(g);
printf("\n"); graph* cg = graphCopy(g);
graphDestroy(g);
graphRemove(cg, e[1]);
graphShow(cg);
printf("\n"); for (int i = 0; i < 5; ++i)
printf("%d: %d\n", e[i].v, e[i].w);
printf("\n"); edge ee[10];
int cnt=graphEdge(cg, ee);
for (int i = 0; i < cnt; ++i)
printf("%d,%d\n", ee[i].v, ee[i].w);
printf("\n"); }
图的邻接矩阵实现(c)的更多相关文章
- 数据结构(12) -- 图的邻接矩阵的DFS和BFS
//////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...
- java 图的邻接矩阵
有向图 在有向图中,结点对<x ,y>是有序的,结点对<x,y>称为从结点x到结点y的一条有向边,因此,<x,y>与<y,x>是两条不同的边.有向图中的 ...
- Java图的邻接矩阵实现
/** * * 图的邻接矩阵实现 * @author John * * @param <T> */ class AMWGraph<T> { private ArrayList& ...
- 图的邻接矩阵存储实现,C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 稠密图(邻接矩阵),并查集,最短路径(Dijkstra,spfa),最小生成树(kruskal,prim)
全部函数通过杭电 1142,1162,1198,1213等题目测试. #include<iostream> #include<vector> #include<queue ...
- _DataStructure_C_Impl:图的邻接矩阵存储
//_DataStructure_C_Impl:邻接矩阵 #include<stdio.h> #include<stdlib.h> #include<string.h&g ...
- c_ 数据结构_图_邻接矩阵
程序主要实现了图的深度遍历和广度遍历. #include <stdio.h> #include <stdlib.h> #include <string.h> #de ...
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径1
import java.util.ArrayList; import java.util.List; // 模块E public class AdjMatrixGraph<E> { pro ...
- 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)
数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...
随机推荐
- iOS设计模式解析(一)工厂方法
工厂方法:定义创建对象的借口,让子类决定实例化哪一个类.工厂方法是一个类的实例化延迟到了子类 例如 :Shoes厂有两个子类(Newbalance.Nike)构建类图如下: 代码实现: #i ...
- [Linked List]Sort List
otal Accepted: 59473 Total Submissions: 253637 Difficulty: Medium Sort a linked list in O(n log n) t ...
- Eclipse选中变量名,相同变量都变色显示 的设置
ava文件的设置"Window"-"preferences"-"Java"-"Editor"-"Mark Oc ...
- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AS" in the equal to operation.
Scenario : 这个问题是我的存储过程中用到临时表时发生的. 应该是sql server 服务器的排序规则 (SQL_Latin1_General_CP1_CI_AS ) 与数据库的排序规则(C ...
- Flex中如何通过设置GridLines对象的horizontalAlternateFill样式交错显示LineSeries图表背景颜色的例子
原文 http://blog.minidx.com/2008/11/27/1652.html 接下来的例子演示了Flex中如何通过设置GridLines对象的horizontalAlternateFi ...
- Annotation 与 HttpClient(5)--Annotation HttpClient
Annotation HttpClient 本内容不保证正确性,如有问题请及时提出 经过前面四篇博客的铺垫,现在给出带有标记的HttpClient的实现. 1. 带标记的HttpClient的 ...
- POJ——多项式的加法
1:多项式加法 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 5000kB 描述 我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起.首先 ...
- 线性表A-B
1.顺序存储 #include<stdio.h> /* 设有两个顺序表A和B,且都递增有序,试写一算法,从A中删除与B中相同的那些元素,即求A-B */ #define getArrayL ...
- 将Dictionary序列化为json数据 、json数据反序列化为Dictionary
需要引用System.Web.Extensions dll类库 /// <summary> /// 将json数据反序列化为Dictionary /// </summary> ...
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp: <body> <s:fielderror /> <form action="${pageContext.request.contextPa ...