美文网首页
图论基础

图论基础

作者: 滨岩 | 来源:发表于2020-02-24 18:23 被阅读0次

图的表示有两种:

邻接矩阵(Adjacency Matrix)和邻接表(Adjacency Lists)

1、邻接表适合表示稀疏图(Sparse Graph)
稀疏图,顶点的个数大于边的个数,邻接表更节约空间

邻接表无向图的表达方式如下:


image.png

邻接表的有向图的表达方式如下:


image.png

2、邻接矩阵适合表示稠密图(Dense Graph)
稠密图 和完全图

邻接矩阵可以表示有向图和无向图
无向图

image.png

有向图


image.png

邻接矩阵完整代码如下:

// 稠密图 - 邻接矩阵
public class DenseGraph {

    private int nodeCount;  // 节点数
    private int edgeCount;  // 边数
    private boolean directed;   // 是否为有向图
    private boolean[][] matrix;      // 图的具体数据

    public DenseGraph(int nodeCount, boolean directed) {
        this.nodeCount = nodeCount;
        this.edgeCount = 0;
        this.directed = directed;
        // g初始化为n*n的布尔矩阵, 每一个matrix[i][j]均为false, 表示没有任和边
        // false为boolean型变量的默认值
        this.matrix = new boolean[nodeCount][nodeCount];
    }

    //返回节点个数
    public int getNodeCount() {
        return nodeCount;
    }

    //返回边的个数
    public int getEdgeCount() {
        return edgeCount;
    }


    public void addEdge(int v, int w) {
        if (v < 0 || v > nodeCount || w < 0 || w > nodeCount) {
            throw new IllegalArgumentException("参数不合法!");
        }

        if (hasEdge(v, w)) {
            return;
        }
        matrix[v][w] = true;
        edgeCount++;

        if (directed) {
            return;
        }

        matrix[w][v] = true;
    }


    public boolean hasEdge(int v, int w) {
        if (v < 0 || v > nodeCount || w < 0 || w > nodeCount) {
            throw new IllegalArgumentException("参数不合法!");
        }

        return matrix[v][w];
    }

}

邻接表完整代码如下:

// 稀疏图 - 邻接表
public class SparseGraph {

    private int nodeCount;  // 节点数
    private int edgeCount;  // 边数
    private boolean direct; // 是否为有向图
    private Vector<Integer>[] graphList; // 图的具体数据


    public SparseGraph(int nodeCount, boolean direct) {
        this.nodeCount = nodeCount;
        this.direct = direct;
        this.edgeCount = 0;

        // g初始化为n个空的vector, 表示每一个g[i]都为空, 即没有任和边
        this.graphList = new Vector[nodeCount];
        for (int i = 0; i < nodeCount; i++) {
            this.graphList[i] = new Vector<>();
        }
    }


    //返回节点个数
    public int getNodeCount() {
        return this.nodeCount;
    }

    //返回边数
    public int getEdgeCount() {
        return this.edgeCount;
    }

    //向图中添加一个边
    public void addEdge(int v, int w) {
        if (v < 0 || v > nodeCount || w < 0 || w > nodeCount) {
            throw new IllegalArgumentException("index is out of bound");
        }
        this.graphList[v].add(w);
        edgeCount++;

        if (direct || v == w) {
            return;
        }
        this.graphList[w].add(v);

    }

    //验证图中是否有从v到w的边
    public boolean hasEdge(int v, int w) {
        if (v < 0 || v > nodeCount || w < 0 || w > nodeCount) {
            throw new IllegalArgumentException("index is out of bound");
        }

        Vector<Integer> vector = this.graphList[v];
        for (int i = 0; i < vector.size(); i++) {
            if (vector.elementAt(v) == w) {
                return true;
            }
        }

        return false;
    }
}

相关文章

  • 图论基础

    图分为有向图,和无向图。 如果图的边数接近顶点数其为稠密图 如果图的边数远远小于顶点数其为稀疏图 表示稠密图一般采...

  • 图论基础

    图的表示有两种: 邻接矩阵(Adjacency Matrix)和邻接表(Adjacency Lists) 1、邻接...

  • 图论基础

    一、图的初识 在一个社交网络中,每个帐号和他们之间的关系构成了一张巨大的网络,就像下面这张图: 那么在电脑中,我们...

  • 图论 基础篇

    一. 图的介绍 说起图这个词,很多人可能首先会想到的就是图片,地图......等,但这里所说的图是一个抽象的概念。...

  • 图论(1)-tarjan算法求强联通分量,割点,桥

    在LC里面的图论题,一般还是非常基础的,BFS,或者Dijkstra 为主。造成其实有很多经典的图论算法运用的不多...

  • 图神经网络03-图与图学习(中)

    在上篇中,我们简单学习了图论的基本概念,图的表示和存储方式,同构图和异构图的分类,以及几个基础的图论算法。在接下来...

  • 图--图论基础(1)

    一.图的简介 1.图是由节点和边构成的 2.图的分类:无向图,有向图 无权图,有权图 3.简...

  • 深度透析图论基础

    图论基础 1、图的定义 图(Graph)是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其...

  • Python基础图论算法

    本来下学期在学姐的强力安利之下选了算法这个课,但是取消了,于是在家打发时间看了edX上的一个法国人讲的算法网课。主...

  • <<数学之美>> part5

    摘要: [图论] [网络爬虫] [图的遍历] 图论 说到图论,必须要提的就是KonigsBerg七桥问题,简单说就...

网友评论

      本文标题:图论基础

      本文链接:https://www.haomeiwen.com/subject/vgnvqhtx.html