zoukankan      html  css  js  c++  java
  • 邻接表&链式前向星

    链式前向星:

    适合点多、边少的情况

    不适用于大量遍历出边的题目(因为cache miss)

    邻接表:

    如果用邻接表来实现的话,一般就用vector嘛,我们都知道vector都是自动扩容的,在空间满了以后,就自动申请多一倍空间。

    对于这样一张有向图:


    输入边的顺序如下:
    1 2
    2 3
    3 4
    1 3
    4 1
    1 5
    4 5

    对于邻接表来说是这样的:
    1 -> 2 -> 3 -> 5
    2 -> 3
    3 -> 4
    4 -> 1 -> 5
    5 ->^

    对于链式前向星来说是这样的:

    edge[0].to = 2; edge[0].next = -1; head[1] = 0;
    edge[1].to = 3; edge[1].next = -1; head[2] = 1;
    edge[2].to = 4; edge[2],next = -1; head[3] = 2;
    edge[3].to = 3; edge[3].next = 0; head[1] = 3;
    edge[4].to = 1; edge[4].next = -1; head[4] = 4;
    edge[5].to = 5; edge[5].next = 3; head[1] = 5;
    edge[6].to = 5; edge[6].next = 4; head[4] = 6;

    邻接表是用链表实现的,可以动态的增加边, 
    而链式前向星是用结构体数组实现的,是静态的,需要一开始知道数据范围,开好数组大小。 
    相比之下,邻接表灵活,链式前向星好写。

  • 相关阅读:
    Git fetch和git pull的区别
    git revert和git reset的区别
    JSF 与 HTML 标签的联系
    3. Decorator
    2. Observer
    1. Strategy
    继承构造函数的执行顺序
    模板特化
    8.1.2 Template instantiation (Accelerated C++)
    std::cin
  • 原文地址:https://www.cnblogs.com/darlingroot/p/10337095.html
Copyright © 2011-2022 走看看