图的表示形式:邻接矩阵和邻接表

本文介绍了图的两种表示方法——邻接矩阵和邻接表。邻接矩阵适用于表示图,尤其是判断两个顶点间是否存在边。在无向图中,邻接矩阵是对称的,而在有向图中则不然。邻接矩阵在内存使用上可能浪费空间,适合存储稠密图。邻接表则在处理包含大量顶点但边数较少的图时更节省内存,但查找特定边的存在和计算入度可能需要更多时间。
摘要由CSDN通过智能技术生成

Adjacency Matrix

A graph G = (V, E) where v= {0, 1, 2, . . .
n-1} can be represented using two dimensional integer array of size n x n.
int adj[20][20] can be used to store a
graph with 20 vertices
adj[i][j] = 1, indicates presence of edge
between two vertices i and j.
adj[i][j] = 0, indicates absence of edge
between two vertices i and j.
  • A graph is represented using square matrix.
  • Adjacency matrix of an undirected graph is

    always a symmetric matrix, i.e. an edge (i, j) implies the edge (j, i).

  • Adjacency matrix of a directed graph is

    never symmetric, adj[i][j] = 1 indicates a directed edge from vertex i to

    vertex j.

Read Previous Article: Graphs: Introduction and Terminology
An example of adjacency matrix
representation of an undirected and directed graph is given below:

Adjacency matrix representation of a
weighted graph

For weighted graph, the matrix adj[ ][ ] is
represented as:
If there is an edge between vertices i and
j then adj[i][j] = weight of the edge (i, j) otherwise adj[i][j] = 0.
An example of representation of weighted
graph is given below:
  • Adjacency matrix representation of graphs

    is very simple to implement.

  • Memory requirement: Adjacency matrix

    representation of a graph wastes lot of memory space. Such matrices are found

    to be very sparse. This representation requires space for n2 elements

    for a graph with n vertices. If the graph has e number of edges then n2 –

    e elements in the matrix will be 0.

  • Presence of an edge between two vertices Vi

    and Vj can be checked in constant time. if(adj[i][j] == 1) then edge is present between vertices i and j, else edge is absent between vertices i and j

  • Degree of a vertex can easily be calculated

    by counting all non-zero entries in the corresponding row of the adjacency

    matrix.

Adjacency List

A graph can also be represented using alinked list. For each vertex, a list of adjacent vertices is maintained using a
linked list. It creates a separate linked list for each vertex Vi in
the graph G = (V, E).
Adjacency list of a graph with n nodes can
be represented by an array of pointers. Each pointer points to a linked list of
the corresponding vertex. Below figure shows the adjacency list representation
of a graph.
  • Adjacency list representation of a graph is

    very memory efficient when the graph has a large number of vertices but very

    few edges.

  • For an undirected graph with n vertices and

    e edges, total number of nodes will be n + 2e. If e is large then due to

    overhead of maintaining pointers, adjacency list representation does not remain

    cost effective over adjacency matrix representation of a graph.

  • Degree of a node in an undirected graph is

    given by the length of the corresponding linked list.

  • Finding indegree of a directed graph

    represented using adjacency list will require O (e) comparisons. Lists pointed

    by all vertices must be examined to find the indegree of a node in a directed

    graph.

  • Checking the existence of an edge between

    two vertices i and j is also time consuming. Linked list of vertex i must be

    searched for the vertex j.

A graph can be represented using a
structure as defined below:
#define MAX 30              //graph has maximum of 30 nodes
typedef struct node
{
               struct
node *next;
               int
vertex;
}node;
node *head[MAX];
If a weighted graph is to be represented
using a adjacency list, then structure “node” should be modified to include the
weight of an edge. Thus the definition of ‘struct node’ is
modified as below:
typedef struct node
{
struct node
*next;
               int
vertex;
               int
weight;
}node;

翻译自: https://www.thecrazyprogrammer.com/2014/03/representation-of-graphs-adjacency-matrix-and-adjacency-list.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值