Table of Contents

  1. [Edge List <-- Adjacency Matrix](# Edge List <-- Adjacency Matrix)
  2. [Edge List --> Adjacency Matrix](# Edge List --> Adjacency Matrix)
  3. [About Adjacency List](#About Adjacency List)

Edge List <-- Adjacency Matrix

'''
ref: https://www.cnblogs.com/sonictl/p/10688533.html
convert adjMatrix into edgelist: 'data/unweighted_edgelist.number' or 'data/weighted_edgelist.number'' input: adjacency matrix with delimiter=', '
it can process:
- Unweighted directed graph
- Weighted directed graph output: edgelist (unweighted and weighted) ''' import numpy as np
import networkx as nx # -------DIRECTED Graph, Unweighted-----------
# Unweighted directed graph:
a = np.loadtxt('data/test_adjMatrix.txt', delimiter=', ', dtype=int)
D = nx.DiGraph(a)
nx.write_edgelist(D, 'data/unweighted_edgelist.number', data=False) # output edges = [(u, v) for (u, v) in D.edges()]
print(edges) # -------DIRECTED Graph, Weighted------------
# Weighted directed graph (weighted adj_matrix):
a = np.loadtxt('data/adjmatrix_weight_sample.txt', delimiter=', ', dtype=float)
D = nx.DiGraph(a)
nx.write_weighted_edgelist(D, 'data/weighted_edgelist.number') # write the weighted edgelist into file # print(D.edges)
elarge = [(u, v, d['weight']) for (u, v, d) in D.edges(data=True) if d['weight'] > 0.]
print(elarge) # class: list # -------UNDIRECTED Graph -------------------
# for undirected graph, simply use:
udrtG = D.to_undirected() '''
test_adjMatrix.txt: (Symmetric matrices if unweighted graph)
---
0, 1, 1, 1, 0, 1, 1, 0
0, 0, 1, 0, 0, 0, 1, 1
0, 0, 0, 1, 1, 0, 0, 0
0, 1, 0, 0, 1, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 1, 0, 0, 0, 0, 0
=== adjmatrix_weight_sample.txt:
---
0, 0.5, 0.5, 0.5, 0, 0.5, 0.5, 0
0, 0, 0.5, 0, 0, 0, 0.5, 0.5
0, 0, 0, 0.5, 0.5, 0, 0, 0
0, 0.5, 0, 0, 0.5, 0.5, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0.5, 0, 0, 0, 0, 0
=== output:
---
[(0, 1), (0, 2), (0, 3), (0, 5), (0, 6), (1, 2), (1, 6), (1, 7), (2, 3), (2, 4), (3, 1), (3, 4), (3, 5), (7, 2)]
[(0, 1, 0.5), (0, 2, 0.5), (0, 3, 0.5), (0, 5, 0.5), (0, 6, 0.5), (1, 2, 0.5), (1, 6, 0.5), (1, 7, 0.5), (2, 3, 0.5), (2, 4, 0.5), (3, 1, 0.5), (3, 4, 0.5), (3, 5, 0.5), (7, 2, 0.5)]
===
'''

Edge List --> Adjacency Matrix

'''
https://networkx.github.io/documentation/networkx-2.2/reference/generated/networkx.linalg.graphmatrix.adjacency_matrix.html ''' import numpy
import networkx as nx # edgelist to adjacency matrix # way1: G=nx.read_edgelist
D = nx.read_edgelist('input/edgelist_sample.txt', create_using=nx.DiGraph(), nodetype=int) # create_using=nx.Graph()
print(D.edges)
print(D.nodes) # way2:
'''
a = numpy.loadtxt('input/edgelist_sample.txt', dtype=int)
edges = [tuple(e) for e in a]
D = nx.DiGraph()
D.add_edges_from(edges) # D.add_edges_from(nodes); D.edges; D.nodes
D.name = 'digraph_sample'
print(nx.info(D)) udrtG = D.to_undirected()
udrtG.name = 'udrt'
print(nx.info(udrtG))
''' # dump to file as adjacency Matrix
A = nx.adjacency_matrix(D, nodelist=list(range(len(D.nodes)))) # nx.adjacency_matrix(D, nodelist=None, weight='weight') # Return type: SciPy sparse matrix
# print(A) # type < SciPy sparse matrix >
A_dense = A.todense() # type-> numpy.matrixlib.defmatrix.matrix
print(A_dense, type(A_dense)) print('--- See two row of matrix equal or not: ---')
print((numpy.equal(A_dense[5], A_dense[6])).all()) # print('to_numpy_array:\n', nx.to_numpy_array(D, nodelist=list(range(len(D.nodes))))) # print('to_dict_of_dicts:\n', nx.to_dict_of_dicts(D, nodelist=list(range(len(D.nodes)))))

About Adjacency LIST

nx.read_adjlist()

Convert Adjacency matrix into edgelist

import numpy as np

#read matrix without head.
a = np.loadtxt('admatrix.txt', delimiter=', ', dtype=int) #set the delimiter as you need
print "a:"
print a
print 'shape:',a.shape[0] ,"*", a.shape[1] num_nodes = a.shape[0] + a.shape[1] num_edge = 0
edgeSet = set() for row in range(a.shape[0]):
for column in range(a.shape[1]):
if a.item(row,column) == 1 and (column,row) not in edgeSet: #get rid of repeat edge
num_edge += 1
edgeSet.add((row,column)) print '\nnum_edge:', num_edge
print 'edge Set:', edgeSet
print ''
for edge in edgeSet:
print edge[0] , edge[1]

Sample Adjacency Matrix Input file:

0, 1, 1, 1, 0, 1, 1, 0
0, 0, 1, 0, 0, 0, 1, 1
0, 0, 0, 1, 1, 0, 0, 0
0, 1, 0, 0, 1, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 1, 0, 0, 0, 0, 0

Convert Adjacency matrix into edgelist的更多相关文章

  1. 路径规划 Adjacency matrix 传球问题

    建模 问题是什么 知道了问题是什么答案就ok了 重复考虑 与 重复计算 程序可以重复考虑  但往目标篮子中放入时,放不放把握好就ok了. 集合 交集 并集 w 路径规划 字符串处理 42423 424 ...

  2. 【leetcode】1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

    题目如下: Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the ...

  3. LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix (最少翻转次数将二进制矩阵全部置为0)

    给一个矩阵mat,每个格子都是0或1,翻转一个格子会将该格子以及相邻的格子(有共同边)全部翻转(0变为1,1变为0) 求问最少需要翻转几次将所有格子全部置为0. 这题的重点是数据范围,比赛结束看了眼数 ...

  4. Adjacency matrix based Graph

    Interface AddVertex(T data) AddEdge(int from, int to) DFS BFS MST TopSort PrintGraph using System; u ...

  5. R matrix 转换为 dataframe

    When I try converting a matrix to a data frame, it works for me: > x <- matrix(1:6,ncol=2,dimn ...

  6. 拉普拉斯矩阵(Laplacian Matrix) 及半正定性证明

    摘自 https://blog.csdn.net/beiyangdashu/article/details/49300479 和 https://en.wikipedia.org/wiki/Lapla ...

  7. Distance matrix

    w https://en.wikipedia.org/wiki/Distance_matrix For example, suppose these data are to be analyzed, ...

  8. 用matalb、python画聚类结果图

    用matlab %读入聚类后的数据, 已经分好级别了,例如前4行是亚洲一流, %-13是亚洲二流,-24是亚洲三流 a=xlsread('C:\Users\Liugengxin\Desktop\1.x ...

  9. OO课程第三次总结QWQ

    调研,然后总结介绍规格化设计的大致发展历史和为什么得到了人们的重视 emmm为这个问题翻遍百度谷歌知乎也没有得到答案,那我就把自己认为最重要的两点简要说明一下吧,欢迎大家补充~ 1.便于完成代码的重用 ...

随机推荐

  1. HTML5制作网页(2)

     <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...

  2. Jdbc来操作事物 完成模拟银行的转账业务

    创建JDBC工具类 package cn.aa4_2.JDBCUtils; import java.io.FileReader; import java.io.IOException; import ...

  3. HOMEWORK1

    回顾你过去将近3年的学习经历 当初你报考的时候是真正喜欢计算机这个专业吗? 当初报考的时候是选择英语和计算机专业,报英语那个学校没去上,就来学了计算机,对计算机专业的感觉介于喜欢和热爱之间,就是说还是 ...

  4. 2018-2019-2 20165313 《网络对抗技术》 Exp6 信息搜集与漏洞扫描

    一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容. (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测. ...

  5. 【puppeteer】前端自动化初探(一)

    一.前提 windows环境的puppeteer环境配置要简单点,mac环境坑竟然有点多,这边稍微提下 二.开发环境 nodejs puppeteer mac 三.简单介绍下puppeteer Pup ...

  6. Python闭包举例

    Python闭包的条件: 1.函数嵌套.在外部函数内,定义内部函数. 2.参数传递.外部函数的局部变量,作为内部函数参数. 3.返回函数.外部函数的返回值,为内部函数. 举例如下: def line_ ...

  7. 微信小程序计算金额长度异常解决办法

    今天在做微信小程序,在测试的时候偶然出现了一些问题,如下图. 心中的一阵不爽猛然袭来,完全是搞事情哈! 然后经过一番探索,用toFixed方法搞定了,此方法是对值进行四舍五入的. 解决后点了一大堆控制 ...

  8. 基于java webRct webSocket 实现点对点视频 (需要源码的请加支付宝好友)

    打开支付宝首页搜“555176706”领红包,即可加好友 <%@ page language="java" pageEncoding="UTF-8"%&g ...

  9. BeanUtils使用

    1.BeanUtils.populate 可以把一个map中的属性拷贝到实体javaBean,例子: Student: package com.cy.model; import org.apache. ...

  10. kong插件应用

    插件概述 插件之于kong,就像Spring中的aop功能.在请求到达kong之后,转发给后端应用之前,你可以应用kong自带的插件对请求进行处理,合法认证,限流控制,黑白名单校验,日志采集等等.同时 ...