"# define d as a nx1 sparse matrix, where n is the number of nodes in the graph. The vector is filled with d(i) = 1 if the i row of the matrix P is filled with zeros, other wise is 0\n",
"# define d as a nx1 sparse matrix, where n is the number of nodes in the graph. The vector is filled with d(i) = 1 if the i row of the matrix P is filled with zeros, other wise is 0\n",
"\n",
"# d is the vector of dangling nodes\n",
"d = sp.sparse.lil_matrix((n,1))\n",
"d = sp.sparse.lil_matrix((n,1))\n",
"for i in range(n):\n",
"for i in range(n):\n",
" if P[i].sum() == 0:\n",
" if P[i].sum() == 0:\n",
@ -108,7 +113,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
@ -129,7 +134,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
@ -140,7 +145,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
@ -152,7 +157,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
@ -175,70 +180,76 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"a = [0.85, 0.9, 0.95, 0.99]\n",
"# list of alpha values, from 0.85 to 0.99 with step 0.01\n",
"tau = 10**-8\n",
"a = []\n",
"max_mv = 1000\n",
"for i in range(85,100):\n",
"\n",
" a.append(i/100)\n",
"# this should return mv (the number of iteration needed for the convergence), and two vector of lenght len(a) called x and r. Where x is the vector of the pagerank and r is the residual vector\n",
"\n",
"\n",
"tau = 10**-6\n",
"max_mv = 100\n",
"\n",
"\n",
"# this should return mv (the number of iteration needed for the convergence), and two vector called x and r. Where x is the vector of the pagerank and r is the residual vector\n",
"\n",
"\n",
"def Algorithm1(Pt, v, tau, max_mv, a: list):\n",
"def Algorithm1(Pt, v, tau, max_mv, a: list):\n",
" # take time of the performance\n",
" start_time = time.time()\n",
"\n",
"\n",
" u = Pt.dot(v) - v \n",
" u = Pt.dot(v) - v \n",
" mv = 1\n",
" mv = 1 # number of iteration\n",
" for i in range(len(a)):\n",
" r = sp.sparse.lil_matrix((n,1)) \n",
" r = sp.sparse.lil_matrix((len(a),1))\n",
" r[i] = a[i]*u\n",
" Res = sp.sparse.lil_matrix((len(a),1))\n",
" Res = sp.sparse.lil_matrix((len(a),1))\n",
" Res[i] = np.linalg.norm(r[i])\n",
" x = sp.sparse.lil_matrix((n,1)) \n",
"\n",
" for i in range(len(a)):\n",
" r = a[i]*(u) \n",
" normed_r = norm(r)\n",
" Res[i] = normed_r \n",
"\n",
"\n",
" if Res[i] > tau:\n",
" if Res[i] > tau:\n",
" x = sp.sparse.lil_matrix((len(a),1))\n",
" x = r + v \n",
" x[i] = r[i] + v\n",
"\n",
" print(\"STARTING THE WHILE LOOP\\n\")\n",
"\n",
" # take the maximum value of the sparse matrix Res\n",
"\n",
"\n",
"\n",
" while max(Res) > tau and mv < max_mv:\n",
" while max(Res) > tau and mv < max_mv:\n",
" u = Pt.dot(u)\n",
" u = Pt*u # should it be the same u of the beginning?\n",
" mv += 1 \n",
" mv += 1 \n",
" print(\"mv = \", mv)\n",
" print(\"max(Res) = \", max(Res))\n",
"\n",
"\n",
" for i in range(len(a)):\n",
" for i in range(len(a)):\n",
" if Res[i] >= tau: \n",
" if Res[i] >= tau: \n",
" r = sp.sparse.lil_matrix((len(a),1))\n",
" r = (a[i]**(mv+1))*(u)\n",
" r[i] = a[i]*u\n",
" Res[i] = norm(r)\n",
" Res = sp.sparse.lil_matrix((len(a),1))\n",
" Res[i] = np.linalg.norm(r[i])\n",
"\n",
"\n",
" if Res[i] > tau:\n",
" if Res[i] > tau:\n",
" x = sp.sparse.lil_matrix((len(a),1))\n",
" x = r + x\n",
" x[i] = r[i] + x[i]\n",
"\n",
" print(\"\\nEND OF THE WHILE LOOP\\n\")\n",
"\n",
" if mv == max_mv:\n",
" print(\"The algorithm didn't converge in \", max_mv, \" iterations\")\n",
" else:\n",
" print(\"The algorithm converged in \", mv, \" iterations\")\n",
"\n",
" print(\"\\nThe execution time is %s seconds\" % (time.time() - start_time))\n",