{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def generiranje_matrik(m, n): \n", " d = m * n # število vrstic in stolpcev\n", " rd = range(d) # obseg indeksov\n", " matrika = Matrix(d, d) # matrika samih ničel\n", " for i in rd:\n", " for j in (i-1, i+1, i-n, i+n):\n", " if j in rd:\n", " matrika[i, j] = 1\n", " return matrika\n", "\n", "def matrika_mreza(m, n): #že vgrajena funkcija, .am() vrne matriko in ne grafa\n", " return graphs.Grid2dGraph(m, n).am()\n", "\n", "def izris_grafa(matrika): #izriše graf za dano matriko\n", " matrika = Matrix(matrika)\n", " G = Graph(matrika)\n", " return G.plot().show()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def prehodna_matrika_M(matrika): #vrne matriko katere (i,j)-ti element prikazuje verjetnost, da se bomo iz i-tega vozlišča premaknili v j-to\n", " return Matrix([r/v for r, v in zip(matrika, sum(matrika))]) # vrstica / vsota vrstice" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def brisanje(j, matrika): #izbriše j-ti stolpec in j-to vrstico\n", " return matrika.delete_columns([j]).delete_rows([j])\n", "\n", "def dodajanje(j, matrika): #doda j-to vrstico in j-ti stolpec (ničle)\n", " return block_matrix([[matrika[:j, :j], 0, matrika[:j, j:]],\n", " [0, Matrix(1, 1), 0],\n", " [matrika[j:, :j], 0, matrika[j:, j:]]])\n", "\n", "def element_H(i, j, matrika): #izračuna (i,j)-ti element matrike H\n", " IM = dodajanje(j, (identity_matrix(matrika.nrows()-1) - brisanje(j, matrika))^-2)\n", " return sum(IM[i, k] * matrika[k, j] for k in range(matrika.nrows()) if k != j)\n", "\n", "def matrika_H(matrika): #vrne celotno matriko H\n", " return Matrix([next(zip(*dodajanje(j, (identity_matrix(matrika.nrows()-1) - brisanje(j, matrika))^-2) * matrika[:, j]))\n", " for j in range(matrika.ncols())]).transpose()\n" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def nakjucni_sprehod_blizine_centralnosti(i,matrika): #sprejme incidenčno matriko!\n", " vsota = 0\n", " n = matrika.nrows()\n", " rd = range(0,n)\n", " M = prehodna_matrika_M(matrika) #izračun matrike M\n", " for j in rd:\n", " vsota += matrika_H(M)[j,i]\n", " return n/vsota\n", "\n", "#def centralno_po_npbc(matrika):\n", "# n = matrika.nrows()\n", "# rd = range(0,n)\n", "# rez = Matrix(n,1)\n", "# for i in rd: #za vsako vozlišče izračunamo nsbc\n", "# print(nakjucni_sprehod_blizine_centralnosti(i,matrika))\n", "# rez[i] = int(nakjucni_sprehod_blizine_centralnosti(i,matrika))\n", "# print(rez[i])\n", "# return rez\n", "\n", "def centralno_po_nsbc(matrika):\n", " n = matrika.nrows()\n", " rd = range(0,n)\n", " najvecji = [0,0]\n", " for i in rd:\n", " if nakjucni_sprehod_blizine_centralnosti(i,matrika) > najvecji[1]:\n", " najvecji = [i][nakjucni_sprehod_blizine_centralnosti(i,matrika)] #ne zna prevest rational v integer, vprašaj vidali\n", " return najvecji" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "A = matrika_mreza(3,6)\n", "#show(nakjucni_sprehod_blizine_centralnosti(1,A),nakjucni_sprehod_blizine_centralnosti(2,A))\n", "#max(nakjucni_sprehod_blizine_centralnosti(1,A),nakjucni_sprehod_blizine_centralnosti(2,A))\n", "#B = [4,5]\n", "#show(B[0])\n", "#centralno_po_nobc(A)\n", "#centralno_po_npbc(A)\n", "#rez = Matrix(18,1)\n", "#type(A)\n", "#(A^3)[1][1]\n", "type(nakjucni_sprehod_blizine_centralnosti(1,A))\n", "#rez[1] = tuple(nakjucni_sprehod_blizine_centralnosti(1,A))\n", "#show(rez)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "def oddaljenost_i(i,matrika): #vrne oddaljenost (najkrajšo pot) i-tega vozlišča do vseh ostalih vozlišč\n", " n = matrika.nrows()\n", " rd = range(0,n)\n", " vektor = Matrix(n,1)\n", " for j in rd: #j != i\n", " if j == i: #spusti\n", " vektor = vektor\n", " elif matrika[i,j] != 0: #sosednja vozlišča\n", " vektor[j] = 1\n", " else:\n", " for l in range(1,n):\n", " if vektor[j] != 0:\n", " vektor = vektor\n", " elif (matrika^l)[i,j] != 0:\n", " vektor[j] = l\n", " return vektor\n", "\n", "def centralnost_i(i,matrika): #centralno vozlišče\n", " return 1/sum(oddaljenost_i(i,matrika)) #sum() ne zna delit, vprašaj vidali" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "[1]\n", "[0]\n", "[1]\n", "[2]\n", "[3]\n", "[4]\n", "[2]\n", "[1]\n", "[2]\n", "[3]\n", "[4]\n", "[5]\n", "[3]\n", "[2]\n", "[3]\n", "[4]\n", "[5]\n", "[6]" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/html": [ "" ], "text/plain": [ "(51)" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" }, { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": { }, "output_type": "execute_result" } ], "source": [ "A = matrika_mreza(3,6)\n", "v=oddaljenost_i(1,A)\n", "show(v)\n", "s=sum(v)\n", "#C=centralnost_i(1,A)\n", "show(s)\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "cannot add edge from 2 to 2 in graph without loops", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0msez2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 37\u001b[0;31m \u001b[0mkrozni_graf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;31m#C.show() # long time\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mkrozni_graf\u001b[0;34m(n, k)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0mk\u001b[0m \u001b[0;34m-=\u001b[0m \u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msez2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0mC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_edges\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 35\u001b[0m \u001b[0msez2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/ext/sage/sage-9.1/local/lib/python3.7/site-packages/sage/graphs/generic_graph.py\u001b[0m in \u001b[0;36madd_edges\u001b[0;34m(self, edges, loops)\u001b[0m\n\u001b[1;32m 10925\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cannot interpret {!r} as graph edge\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10926\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mloops\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mu\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 10927\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_backend\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_edge\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mu\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_directed\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10928\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10929\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msubdivide_edge\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/ext/sage/sage-9.1/local/lib/python3.7/site-packages/sage/graphs/base/sparse_graph.pyx\u001b[0m in \u001b[0;36msage.graphs.base.sparse_graph.SparseGraphBackend.add_edge (build/cythonized/sage/graphs/base/sparse_graph.c:16220)\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1358\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1359\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mu_int\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mv_int\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_loops\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1360\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"cannot add edge from {u!r} to {v!r} in graph without loops\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1361\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1362\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmultiple_edges\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: cannot add edge from 2 to 2 in graph without loops" ] } ], "source": [ "\n", "#def 3Dmreza(m,n,z):\n", "# d = m*n*z #dimenzije matrike\n", "# rd = range(d)\n", "# matrika = Matrix(d, d) # matrika samih ničel\n", "\n", "\n", "def generator_binarnih_dreves(k): #vrne binarno drevo \n", " G = graphs.BalancedTree(2,k)\n", " show(G)\n", " return G\n", "\n", "#def bd_matrika(k):\n", "# return graphs.Grid2dGraph(m, n).am()\n", "\n", "\n", "import networkx\n", "import random\n", "\n", "#def krozni_graf(n,k): #n vozlisc, vsako ima k povezav\n", "# sez1 = list(range(0,n))\n", "# #n = networkx.cycle_graph(n)\n", "# C = graphs.CycleGraph(n)\n", "# sez2 = []\n", "# if k < n - 2:\n", "# for i in C:\n", "# while k>0:\n", "# r = random.choice(sez1)\n", "# if r == i-1 or r == i+1 or r == i:\n", "# pass\n", "# else:\n", "# sez2.append(r)\n", "# k -= 1\n", "# for j in sez2:\n", "# C.add_edges([(j, i)])\n", " sez2 = []\n", " return C.show()\n", "krozni_graf(8,2)\n", " \n", "#C.show() # long time\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "#def sprehod(matrika,v1,v2):\n", "# a = 0\n", "# while v1 < v2:\n", "# for v1 in matrika:\n", "# for j in matrika:\n", "# while j < matrika.nrows():\n", "# b = 0\n", "# if matrika[v1][j] == 1:\n", "# j += 1\n", "# b = j\n", "# else:\n", "# j += 1\n", "# v1 = b\n", "# a += 1\n", "# return a\n", "\n", "\n", "def oddaljenost_i_j(matrika,i,j): #izračuna dolžino najkrajše poti med i in j\n", " n = matrika.nrows()\n", " rd = range(0,n)\n", " razdalja = 0\n", " if matrika[i,j] != 0:\n", " razdalja = 1\n", " else:\n", " for l in range(2,n):\n", " if (matrika^l)[i,j] != 0:\n", " while razdalja == 0:\n", " razdalja = l\n", " return razdalja\n", "\n", "def povprecje_sprehodov(matrika,k): #sešteje najkrajše razdalje do drugih vozlišč in to deli s številom vozlišč-1\n", " koraki = 0\n", " for i in range(0, matrika.nrows):\n", " if i != k:\n", " koraki += oddaljenost_i_j(matrika,i,k)\n", " else:\n", " pass\n", " return koraki/(matrika.nrows()-1) #se poglej malo mislim da ne klices prav funkcije\n", "\n", "\n", "\n", "import random\n", "\n", "def nakljucni_sprehod(matrika, k):\n", " sez2 = list(range(0, (matrika.nrows()))) #seznam neobiskanih vozlišč\n", " zacetno = k\n", " a = len(sez2)\n", " koraki = 0\n", " koncni_sez = [0] * a\n", " koncni_sez[zacetno] = 1 #v to vozlišče ni potrebno priti, na koncu popravimo na 0\n", " sez2.remove(zacetno) #odstranimo začetno vozlišče, ker v njega ni potrebno priti\n", " while len(sez2) > 0:\n", " sez3 = []\n", " for k in range(0,matrika.nrows()):\n", " for j in range(0,matrika.nrows()):\n", " if matrika[k,j] != 0: #naredimo seznam sosednjih vozlišč\n", " sez3.append(j)\n", " else:\n", " pass\n", " c = random.choice(sez3) #naključno izbere v kako vozlišče se premaknemo\n", " k = c #k označuje trenutno vozlišče\n", " sez3 = []\n", " koraki += 1\n", " if koncni_sez[c] == 0:\n", " koncni_sez[c] = koraki #dodamo število korakov, ki smo jih potrebovali, da smo prišli do tega vozlišča\n", " sez2.remove(c) #in ga odstranimo iz seznama neobiskanih vozlišč\n", " else:\n", " pass\n", " #for i in sez2:\n", " # if sez2[i] == c:\n", " # sez2.pop(c)\n", " # else:\n", " # pass\n", " koncni_sez[zacetno] = 0 #nastavimo začetno vozlišče nazaj na 0\n", " return koncni_sez\n", "\n", "\n", "def n_nakljucnih_sprehodov(matrika,k,n): #funkcija naredi n naključnih sprehodov iz k-tega vozlišča\n", " d = matrika.nrows()\n", " povp = [0] * d\n", " for ponovitev in range(0,n):\n", " v = nakljucni_sprehod(matrika,k)\n", " for i in range(0,d):\n", " povp[i] += v[i]\n", " povp[:] = [x / n for x in povp] #deli vse elemente z n; da dobimo povprečno dolžino\n", " return povp\n", "\n", "def dolzina_vseh(matrika,n): #naredi n naključnih sprehodov; vrne povprečno hitrost, s katero procesi naključnega sprehajanja dosežejo vozlišče iz drugih vozlišč v omrežju\n", " d = matrika.nrows()\n", " povp = [0] * d\n", " for vozlisce in range(0,d):\n", " v = n_nakljucnih_sprehodov(matrika,vozlisce,n)\n", " for i in range(0,d):\n", " povp[i] += v[i]\n", " povp[:] = [x / (n-1)*d for x in povp] #deli vse elemente s številom sprehodov; da dobimo povprečno dolžino\n", " return povp\n", "\n", "def nsbc(matrika,n):\n", " rez = dolzina_vseh(matrika,n)\n", " rez[:] = [1 / x for x in rez]\n", " return rez" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5/2103" ] }, "execution_count": 91, "metadata": { }, "output_type": "execute_result" } ], "source": [ "#v=oddaljenost_i(1,A)\n", "#u=oddaljenost_i_j(A,1,15)\n", "#show(v)\n", "#show(u)\n", "#izris_grafa(A)\n", "#nakljucni_sprehod(A, 2)\n", "#n_nakljucnih_sprehodov(A,2,500)\n", "a=nsbc(A,10)\n", "max(a)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "1320/62341 220/6651 1320/30781" ] }, "execution_count": 89, "metadata": { }, "output_type": "execute_result" } ], "source": [ "show(nakjucni_sprehod_blizine_centralnosti(0,A),nakjucni_sprehod_blizine_centralnosti(1,A),nakjucni_sprehod_blizine_centralnosti(2,A))" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.1", "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 1, "url": "https://www.sagemath.org/" } }, "name": "sage-9.1" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }