• 图论与网络模型——基于R


    图的定义

    从直观上看,所谓图就是由点与边组成的图形,如下图所示:
    在这里插入图片描述

    定义:图G是一个偶对(V,E),其中V是一个非空集合,其元素u属于V称为图的定点;E是由V中的点构成的点对,E的元素e称为图的变或弧。若e是无序对,则称G为无向图,否则则成为有向图。若e=(u,v),则称u为e的起点,v为e的终点。称去掉有向图的方向得到的图为基础图

    构建图中的R函数

    在igraph中可用graph函数构件图,其使用格式为:

    make_graph(
      edges,
      ...,
      n = max(edges),
      isolates = NULL,
      directed = TRUE,
      dir = directed,
      simplify = TRUE
    )
    
    make_directed_graph(edges, n = max(edges))
    
    make_undirected_graph(edges, n = max(edges))
    
    directed_graph(...)
    
    undirected_graph(...)
    Arguments
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    其中:

    • edges为由顶点符号构成的向量,表示图的边,其中奇数点表示边的起点,偶数点表示终点
    • n 为整数,默认值为最大边数
    • directed为逻辑变量,表示是否为有向图
      graph函数返回值为图的对象,如果将图可视化,需要用到plot函数
      比如我们这两幅图:
    #install.packages("igraph")
    library(igraph)
    e=c(1,2,2,2,2,4,1,4,3,1,3,4,4,3,4,5)
    par(mfrow=c(1,2))#把图形分割为两部分
    enames <- paste("e",1:8,sep="")
    g1 <- graph(e,directed = F)
    g2 <- graph(e)
    plot(g1,layout=layout.circle,edge.label=enames)
    plot(g2,layout=layout.circle,edge.label=enames)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    • edge.label=enames表示绘图时表明边的标号
    • layout=layout.circle表明图中顶点的布局类似于一个圆

    把无向图变为有向图

    如图所示,把左边的这个图变成右边:

    as.directed(graph,mode=c("mutual","arbitrary","each","random"))
    
    • 1
    • 参数graph为作图的对象,mode为字符串。取“mutual”(默认值)表示为木条边增加两个方向,取“arbitrary”表示为每条边任意添加一个方向
    library(tidyverse)
    plot(g1,layout=layout.circle,edge.label=enames)#无向图
    as.directed(g1,mode="mutual") %>% plot(layout=layout.circle,edge.label=enames)
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    构建空图

    graph.empty函数

    graph.empty(n=0,directed = T) %>% plot()
    
    • 1

    结果如下:
    在这里插入图片描述

    在图上添加边或者点

    • add.edges(): 添加边
    • add.vertices(): 添加点
      例题
      使用igraph的函数构建图,图G共有10个顶点,顶点用小写英文字母表示,共20条边,边的连接方式如下:
      1,2,1,3,2,3,3,5,3,4,3,6,3,7,4,5,4,9,5,1,5,6,7,1,7,8,
      8,2,8,5,8,9,9,5,9,10,10,6
    E <- c(1,2,1,3,2,3,3,5,3,4,3,6,3,7,4,5,4,9,5,1,5,6,7,1,7,8,
           8,2,8,5,8,9,9,5,9,10,10,6)
    g3 <- graph.empty()+vertices(letters[1:10])
    g3 <-g3+ edges(E) 
    plot(g3,layout=layout.circle)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    简单图与完全图

    用到graph.fromula函数
    graph.full()函数
    graph.ring()函数

    graph.formula(A-+B-+C) %>% plot()
    graph.formula(A+-B-+C) %>% plot()
    graph.formula(A+-B--C) %>% plot()
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    在这里插入图片描述

    graph.lattice(c(2,2,2)) %>% plot()
    graph.full(5) %>% plot()
    graph.ring(10) %>% plot()
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    经济数据预测 | Python实现ELM极限学习机股票价格时间序列预测
    【排序算法】详解直接插入排序和希尔排序原理及其性能分析
    javaweb基于ssm的仓库管理系统
    leetcode 746. 使用最小花费爬楼梯
    ConcurrentModificationException日志关键字报警引发的思考
    客户端Socket传输
    ES京东搜索
    控价为什么一定要先监测价格
    【Android】导入三方jar包/系统的framework.jar
    [附源码]Python计算机毕业设计成绩管理与学情分析系统
  • 原文地址:https://blog.csdn.net/qq_54423921/article/details/126193669