w

Graph and its representations - GeeksforGeeks
http://www.geeksforgeeks.org/graph-and-its-representations/

// A C Program to demonstrate adjacency list representation of graphs

#include <stdio.h>
#include <stdlib.h> // A structure to represent an adjacency list node
struct AdjListNode {
int dest;
struct AdjListNode* next;
}; // A structure to represent an adjacency list
struct AdjList {
struct AdjListNode *head; // pointer to head node of list
}; // A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph {
int V;
struct AdjList* array;
}; // A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
} // A utility function that creates a graph of V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V; // Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); // Initialize each adjacency list as empty by making head as NULL
int i;
for (i = ; i < V; ++i)
graph->array[i].head = NULL; return graph;
} // Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest) {
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
} // A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph) {
int v;
for (v = ; v < graph->V; ++v) {
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl) {
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
} // Driver program to test above functions
int main() {
// create the graph given in above fugure
int V = ;
struct Graph* graph = createGraph(V);
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , );
addEdge(graph, , ); // print the adjacency list representation of the above graph
printGraph(graph); return ;
}

A C Program to demonstrate adjacency list representation of graphs的更多相关文章

  1. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  2. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  3. Graph I - Graph

    Graph There are two standard ways to represent a graph G=(V,E)G=(V,E), where VV is a set of vertices ...

  4. 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  5. [LeetCode] Fizz Buzz 嘶嘶嗡嗡

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  6. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  7. LeetCode 412. Fizz Buzz

    Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...

  8. class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01

    412. Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n. But fo ...

  9. LeetCode Fizz Buzz

    原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...

随机推荐

  1. Django开发博客(七)——markdown优化

    背景 上一次把markdown集成之后.发现还是有非常多问题. 这次须要做一些优化. 1.markdown与普通文本的差别显示. 2.添加点击量的统计 3.加入名片卡的滑动 版本号相关 操作系统:Ma ...

  2. swift算法手记-10

    http://blog.csdn.net/myhaspl private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl ...

  3. The fundamental differences between "GET" and "POST"

    The HTML specifications technically define the difference between "GET" and "POST&quo ...

  4. ios 第三方qq登陆,号码禁止授权

    在以下加入測试账号就可以 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWxpbmNleGlhb2hhbw==/font/5a6L5L2T/fontsiz ...

  5. openstack架构简单介绍J版(更新中)

    title : OPENSTACK架构简单介绍 openstack的发展及历史 openstack是什么? OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算‎软件,以A ...

  6. linux归档压缩命令

    1.tar     tar    -cf    output.tar    file1.txt     file2.txt ..     tar    -rvf    output.tar    fl ...

  7. 关于struts、spring 和 hibernate的说明

    struts 是 web 框架 (jsp/action/actionfrom) hibernate 是 orm框架,处于持久层. spring 是容器框架,用于配置bean,并维护bean之间关系的框 ...

  8. vivado2016.2下系统自带DDR3 ip例程仿真运行

    背景:从ISE14.7迁移到vivado2016.2. xilinx的软件改的真是不一般的大.两个软件操作差距真是让人想骂人.由于项目需要,准备调试DDR3.对于新手来说,例化一个DDR3 ip.如果 ...

  9. Scrapy系列教程(3)------Spider(爬虫核心,定义链接关系和网页信息抽取)

    Spiders Spider类定义了怎样爬取某个(或某些)站点.包含了爬取的动作(比如:是否跟进链接)以及怎样从网页的内容中提取结构化数据(爬取item). 换句话说.Spider就是您定义爬取的动作 ...

  10. java-FileDemo

    关于file类的使用:文件的增删查 package com.example; import java.io.File; import java.io.IOException; public class ...