Undirected Python graphs. The incidence matrix

An undirected graph is set from a file in the form

6 5
1 2
3 2
4 1
3 4
5 2

Where the first row is the number of vertices and edges, and the remaining rows are the edges themselves, you need to output the incidence matrix of this graph, and the adjacency matrix, implemented via NumPy:

class mywindow(QtWidgets.QMainWindow):
    def __init__(self):
        self.Vertix = ""
        self.Edge = ""
        self.Vertixs = []
        self.matrix = []
        self.sums = []
        super(mywindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.showDialog)
        self.ui.pushButton_5.clicked.connect(self.Find_adjacency)
        self.ui.pushButton_3.clicked.connect(self.digit)
        self.ui.pushButton_4.clicked.connect(self.isolated_uniformed)


    def showDialog(self):

        fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Open file', '/home')[0]
        f = open(fname, 'r')

        with f:
            self.Vertexs = [ line.split() for line in f ]    

            self.Vertex = self.Vertexs[0][0]
            self.Edge = self.Vertexs[0][1]
            del self.Vertexs[0]
            print(self.Vertex,self.Edge)
            print(self.Vertexs)


    def Find_Smegnosti(self):
        a = []
        b = []
        for i in self.Vertexs:
            a.append(int(i[0]))
            b.append(int(i[1]))
        print(a)
        print(b)
        self.matrix = np.zeros((int(self.Vertex)+1,int(self.Vertex)+1))
        self.matrix[a,b] = 1
        self.matrix[b,a] = 1
        self.matrix = np.delete(self.matrix, (0), axis=0) 
        self.matrix= np.delete(self.matrix, (0), axis=1 ) 
        for i in range(len(self.matrix)):
            np.delete(self.matrix[i],0)
        self.ui.label_2.setText(str(self.matrix))

How to find the incidence matrix with edges and adjacency matrix?

Author: VladilsavOl, 2020-02-16

1 answers

In fact, you have already been given the incidence matrix, only in a compressed form.

For example, the input data 4 1 should be turned into a column of 6 elements, where the first and fourth ones are ones, and the rest are zeros.

The easiest way is to create a null matrix and then mark the corresponding vertices with a pair of ones for each edge.

 1
Author: MBo, 2020-02-17 05:34:17