From 9a3e10f3274361176b80ed14ea8512a442609759 Mon Sep 17 00:00:00 2001 From: waldeyr Date: Wed, 20 Apr 2022 07:07:08 -0300 Subject: [PATCH 01/12] handshaking lemma; --- ED2.ipynb | 81 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index b4a44ba..9007d0e 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 15, + "execution_count": 41, "id": "d54522d8", "metadata": {}, "outputs": [ @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 42, "id": "8b003e19", "metadata": {}, "outputs": [], @@ -60,19 +60,22 @@ " return arestasDeSaida\n", " \n", " def getArestasEntrada(self):\n", - " if self.__direciobado == False:\n", + " if self.__direcionado == False:\n", " return self.__arestas\n", " arestasSaida = []\n", - " for aresta in sel.__arestas:\n", + " for aresta in self.__arestas:\n", " if aresta.getvDestino() == self:\n", " arestasSaida.append(aresta)\n", " return arestasSaida\n", + " \n", + " def getGrau(self):\n", + " return len(self.getArestasEntrada()+self.getArestasSaida())\n", " " ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 43, "id": "5c3677e7", "metadata": {}, "outputs": [], @@ -86,7 +89,6 @@ " self.__vOrigem.adicionarAresta(self)\n", " self.__vDestino.adicionarAresta(self)\n", " \n", - " \n", " def getvOrigem(self):\n", " return self.__vOrigem\n", " def getvDestino(self):\n", @@ -97,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 44, "id": "d3d8fa70", "metadata": {}, "outputs": [], @@ -118,12 +120,20 @@ " return self.__vertices\n", " \n", " def getArestas(self):\n", - " return self.__arestas" + " return self.__arestas\n", + " \n", + " def HandShakingLemma(self):\n", + " somaGraus = 0\n", + " for v in self.getVertices():\n", + " somaGraus += v.getGrau()\n", + " \n", + " return somaGraus == 2*len(self.getArestas())\n", + " #return sum(v.getGrau() for v in self.getVertices()) == 2*len(self.getArestas())" ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 45, "id": "91b986a7", "metadata": {}, "outputs": [], @@ -140,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 46, "id": "2db17f8a", "metadata": {}, "outputs": [], @@ -152,29 +162,7 @@ }, { "cell_type": "code", - "execution_count": 31, - "id": "1c2d3a62", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "3\n", - "1\n", - "4\n" - ] - } - ], - "source": [ - "for v in G.getVertices():\n", - " print(v.getDado())" - ] - }, - { - "cell_type": "code", - "execution_count": 32, + "execution_count": 47, "id": "28e99364", "metadata": {}, "outputs": [ @@ -182,10 +170,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "4 ---> 1\n", + "2 ---> 3\n", "1 ---> 2\n", "3 ---> 4\n", - "2 ---> 3\n" + "4 ---> 1\n" ] } ], @@ -198,11 +186,24 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "6fd4c082", + "execution_count": 48, + "id": "1c2d3a62", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "G.HandShakingLemma()" + ] } ], "metadata": { @@ -221,7 +222,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.12" + "version": "3.6.13" } }, "nbformat": 4, From fb45eff4f16dc19e0bad721b4f84781b9338e8f0 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:23:06 -0300 Subject: [PATCH 02/12] commit do Karllos --- ED2.ipynb | 106 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 24 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index 9007d0e..8ae90ae 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 41, + "execution_count": 18, "id": "d54522d8", "metadata": {}, "outputs": [ @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 92, "id": "8b003e19", "metadata": {}, "outputs": [], @@ -69,13 +69,12 @@ " return arestasSaida\n", " \n", " def getGrau(self):\n", - " return len(self.getArestasEntrada()+self.getArestasSaida())\n", - " " + " return len(self.getArestasSaida())+ len(self.getArestasEntrada())" ] }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 93, "id": "5c3677e7", "metadata": {}, "outputs": [], @@ -89,6 +88,7 @@ " self.__vOrigem.adicionarAresta(self)\n", " self.__vDestino.adicionarAresta(self)\n", " \n", + " \n", " def getvOrigem(self):\n", " return self.__vOrigem\n", " def getvDestino(self):\n", @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 94, "id": "d3d8fa70", "metadata": {}, "outputs": [], @@ -122,18 +122,20 @@ " def getArestas(self):\n", " return self.__arestas\n", " \n", - " def HandShakingLemma(self):\n", + " def checkHandShakingLemma(self):\n", " somaGraus = 0\n", " for v in self.getVertices():\n", - " somaGraus += v.getGrau()\n", - " \n", - " return somaGraus == 2*len(self.getArestas())\n", - " #return sum(v.getGrau() for v in self.getVertices()) == 2*len(self.getArestas())" + " somaGraus+= v.getGrau()\n", + " print(somaGraus)\n", + " if somaGraus == len(self.getArestas())*2:\n", + " return True\n", + " else:\n", + " return False" ] }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 95, "id": "91b986a7", "metadata": {}, "outputs": [], @@ -144,27 +146,48 @@ "v4 = Vertice(4)\n", "a1 = Aresta( v1, v2, 10, True )\n", "a2 = Aresta( v2, v3, 20, True )\n", - "a3 = Aresta( v3, v4, 30, True )\n", - "a4 = Aresta( v4, v1, 40, True )" + "a3 = Aresta( v3, v4, 30, True )" ] }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 96, "id": "2db17f8a", "metadata": {}, "outputs": [], "source": [ "G = Grafo()\n", "G.setVertices({v1, v2, v3, v4})\n", - "G.setArestas({a1, a2, a3, a4})" + "G.setArestas({a1, a2, a3})" ] }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 97, "id": "28e99364", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "1\n", + "3\n" + ] + } + ], + "source": [ + "for v in G.getVertices():\n", + " print(v.getDado())" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "29d5dcfe", + "metadata": {}, "outputs": [ { "name": "stdout", @@ -172,8 +195,7 @@ "text": [ "2 ---> 3\n", "1 ---> 2\n", - "3 ---> 4\n", - "4 ---> 1\n" + "3 ---> 4\n" ] } ], @@ -186,24 +208,60 @@ }, { "cell_type": "code", - "execution_count": 48, - "id": "1c2d3a62", + "execution_count": 99, + "id": "3ebc1c5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1.getGrau()" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "4690659b", "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + }, { "data": { "text/plain": [ "True" ] }, - "execution_count": 48, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "G.HandShakingLemma()" + "G.checkHandShakingLemma()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e72b83e0", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -222,7 +280,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.13" + "version": "3.9.7" } }, "nbformat": 4, From d94c12f49194125444f5fd207cce7befe66dced2 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 4 May 2022 11:05:46 -0300 Subject: [PATCH 03/12] aula --- ED2.ipynb | 300 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 252 insertions(+), 48 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index 8ae90ae..9fab0ed 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 18, + "execution_count": 1, "id": "d54522d8", "metadata": {}, "outputs": [ @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 16, "id": "8b003e19", "metadata": {}, "outputs": [], @@ -34,15 +34,15 @@ "class Vertice:\n", " \n", " def __init__(self, valor, direcionado=True):\n", - " self.__dado = valor\n", + " self.__valor = valor\n", " self.__direcionado = direcionado\n", " self.__arestas = set()\n", " \n", - " def getDado(self):\n", - " return self.__dado\n", + " def getValor(self):\n", + " return self.__valor\n", " \n", - " def setDado(self, valor):\n", - " self.__dado = valor\n", + " def setValor(self, valor):\n", + " self.__valor = valor\n", " \n", " def getArestas(self):\n", " return self.__arestas\n", @@ -69,41 +69,47 @@ " return arestasSaida\n", " \n", " def getGrau(self):\n", - " return len(self.getArestasSaida())+ len(self.getArestasEntrada())" + " return len(self.getArestasSaida())+ len(self.getArestasEntrada())\n", + " \n", + " def getAdjacentes(self, v):\n", + " listaVerticesAdjacentes = []\n", + " for arestas_de_saida in v.getArestasSaida():\n", + " listaVerticesAdjacentes.append(arestas_de_saida.getvDestino())\n", + " return listaVerticesAdjacentes" ] }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 17, "id": "5c3677e7", "metadata": {}, "outputs": [], "source": [ "class Aresta:\n", - " def __init__(self, vOrigem, vDestino, valor, direcionada=True):\n", + " def __init__(self, vOrigem, vDestino, peso, direcionada=True):\n", " self.__vOrigem = vOrigem\n", " self.__vDestino = vDestino\n", - " self.__valor = valor\n", + " self.__peso = peso\n", " self.__direcionada = direcionada\n", " self.__vOrigem.adicionarAresta(self)\n", " self.__vDestino.adicionarAresta(self)\n", " \n", - " \n", " def getvOrigem(self):\n", " return self.__vOrigem\n", " def getvDestino(self):\n", " return self.__vDestino\n", " def getValor(self):\n", - " return self.__valor" + " return self.__peso" ] }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 18, "id": "d3d8fa70", "metadata": {}, "outputs": [], "source": [ + "from collections import deque\n", "class Grafo:\n", " def __init__(self, direcionado=True):\n", " self.__vertices = set()\n", @@ -119,6 +125,12 @@ " def getVertices(self):\n", " return self.__vertices\n", " \n", + " def getVerticeByValor(self, valor):\n", + " for v in self.__vertices:\n", + " if v.getValor() == valor:\n", + " return v\n", + " return None\n", + " \n", " def getArestas(self):\n", " return self.__arestas\n", " \n", @@ -126,16 +138,60 @@ " somaGraus = 0\n", " for v in self.getVertices():\n", " somaGraus+= v.getGrau()\n", - " print(somaGraus)\n", " if somaGraus == len(self.getArestas())*2:\n", " return True\n", " else:\n", - " return False" + " return False\n", + " \n", + " def dfs(self, graph, v, visitados=[]):\n", + " if v not in visitados:\n", + " visitados.append(v) \n", + " if len(v.getAdjacentes(v)) == 0:\n", + " self.dfs(graph, next(iter(graph.getVertices())), visitados)\n", + " else: # vertice escolhido tem adjacentes\n", + " for adjacente in v.getAdjacentes(v): \n", + " if adjacente not in visitados: \n", + " self.dfs(graph, adjacente, visitados) \n", + " return visitados\n", + " \n", + " def bfs(self, v, visitados = [], fila = deque([])):\n", + " fila.append(v) \n", + " if v not in visitados: \n", + " visitados.append(v)\n", + " while fila: \n", + " vertice = fila.popleft() \n", + " if len(vertice.getArestasSaida()) == 0: \n", + " self.bfs(next(iter(self.getVertices())), visitados, fila) \n", + " else:\n", + " for e in vertice.getArestasSaida(): \n", + " vAdjacente = e.getvDestino()\n", + " if vAdjacente not in visitados: \n", + " visitados.append(vAdjacente) \n", + " fila.append(vAdjacente) \n", + " return visitados\n", + " \n", + " def buscarPorValor(self, valor):\n", + " for v in self.bfs(next(iter(self.getVertices())), visitados = [], fila = deque([])):\n", + " if valor == v.getValor():\n", + " return valor\n", + " return None\n", + " \n", + " def Euler(self, graph):\n", + " impar = 0\n", + " for v in graph.getVertices():\n", + " if v.getGrau() %2 != 0:\n", + " impar += 1\n", + " if impar != 0 >= 2:\n", + " return True \n", + " else:\n", + " return False\n", + "\n", + " " ] }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 19, "id": "91b986a7", "metadata": {}, "outputs": [], @@ -144,26 +200,29 @@ "v2 = Vertice(2)\n", "v3 = Vertice(3)\n", "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", "a1 = Aresta( v1, v2, 10, True )\n", "a2 = Aresta( v2, v3, 20, True )\n", - "a3 = Aresta( v3, v4, 30, True )" + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True )" ] }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 20, "id": "2db17f8a", "metadata": {}, "outputs": [], "source": [ "G = Grafo()\n", - "G.setVertices({v1, v2, v3, v4})\n", - "G.setArestas({a1, a2, a3})" + "G.setVertices({v1, v2, v3, v4, v5})\n", + "G.setArestas({a1, a2, a3, a4, a5})" ] }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 21, "id": "28e99364", "metadata": {}, "outputs": [ @@ -171,21 +230,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "2\n", - "4\n", - "1\n", - "3\n" + "5\t3\t4\t1\t2\t" ] } ], "source": [ "for v in G.getVertices():\n", - " print(v.getDado())" + " print(v.getValor(), end=\"\\t\")" ] }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 22, "id": "29d5dcfe", "metadata": {}, "outputs": [ @@ -193,32 +249,30 @@ "name": "stdout", "output_type": "stream", "text": [ - "2 ---> 3\n", - "1 ---> 2\n", - "3 ---> 4\n" + "1 ---> 2\t4 ---> 5\t2 ---> 3\t4 ---> 1\t3 ---> 4\t" ] } ], "source": [ "for a in G.getArestas():\n", - " print(a.getvOrigem().getDado(), end=\"\")\n", + " print(a.getvOrigem().getValor(), end=\"\")\n", " print(\" ---> \", end=\"\")\n", - " print(a.getvDestino().getDado())" + " print(a.getvDestino().getValor(), end=\"\\t\")" ] }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 23, "id": "3ebc1c5d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1" + "2" ] }, - "execution_count": 99, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -229,24 +283,17 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 24, "id": "4690659b", "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6\n" - ] - }, { "data": { "text/plain": [ "True" ] }, - "execution_count": 100, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -257,11 +304,168 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "e72b83e0", + "execution_count": 25, + "id": "1e0f4098", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Busca em profundidade, iniciando com o vértice 2:\n", + "2\t3\t4\t5\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 5:\n", + "5\t2\t3\t4\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 3:\n", + "3\t4\t5\t2\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 6:\n", + "6\t2\t3\t4\t5\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 4:\n", + "4\t5\t2\t3\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t5\t6\t\n", + ".................................................\n" + ] + } + ], + "source": [ + "v1 = Vertice(1)\n", + "v2 = Vertice(2)\n", + "v3 = Vertice(3)\n", + "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", + "v6 = Vertice(6)\n", + "a1 = Aresta( v1, v2, 10, True )\n", + "a2 = Aresta( v2, v3, 20, True )\n", + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True ) \n", + "a6 = Aresta( v4, v6, 60, True ) \n", + "G = Grafo()\n", + "G.setVertices({v1, v2, v3, v4, v5,v6})\n", + "G.setArestas({a1, a2, a3, a4, a5, a6})\n", + "for vertice in G.getVertices():\n", + " print(f\"Busca em profundidade, iniciando com o vértice {vertice.getValor()}:\")\n", + " for v in G.dfs(G, vertice, visitados=[]):\n", + " print(str(v.getValor())+\"\\t\", end=\"\")\n", + " print(\"\\n.................................................\") " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "b7459764", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Busca em largura, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t5\t6\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 4:\n", + "4\t5\t6\t1\t2\t3\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 3:\n", + "3\t4\t5\t6\t1\t2\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 2:\n", + "2\t3\t4\t5\t6\t1\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 5:\n", + "5\t1\t2\t3\t4\t6\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 6:\n", + "6\t1\t2\t3\t4\t5\t\n", + "............................................\n" + ] + } + ], + "source": [ + "v1 = Vertice(1)\n", + "v2 = Vertice(2)\n", + "v3 = Vertice(3)\n", + "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", + "v6 = Vertice(6)\n", + "a1 = Aresta( v1, v2, 10, True )\n", + "a2 = Aresta( v2, v3, 20, True )\n", + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True ) \n", + "a6 = Aresta( v4, v6, 60, True ) \n", + "G = Grafo()\n", + "G.setVertices({v1, v2, v3, v4, v5,v6})\n", + "G.setArestas({a1, a2, a3, a4, a5, a6})\n", + "for vertice in G.getVertices():\n", + " print(f\"Busca em largura, iniciando com o vértice {vertice.getValor()}:\")\n", + " for v in G.bfs(vertice, visitados = [], fila = deque([])):\n", + " print(str(v.getValor())+\"\\t\", end=\"\")\n", + " print(\"\\n............................................\") " + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "b29de58e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "não é um grafo euleriano\n" + ] + } + ], + "source": [ + "if G.Euler(G):\n", + " print(\"é um grafo euleriano\")\n", + "else:\n", + " print(\"não é um grafo euleriano\")" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "30ca64af", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Valor procurado: 7\n", + "7 não encontrado no grafo\n" + ] + } + ], + "source": [ + "valor = int(input(\"Valor procurado: \"))\n", + "if G.buscarPorValor(valor) == None:\n", + " print(f\"{valor} não encontrado no grafo\")\n", + "else:\n", + " print(f\"{valor} encontrado no grafo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "e62c1503", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import pathlib\n", + "pathlib." + ] } ], "metadata": { From 26c7b15bf26ca20b6926ae6d0a6f782c16e85feb Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 4 May 2022 11:07:34 -0300 Subject: [PATCH 04/12] aula --- ED2.ipynb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index 9fab0ed..1f9f6ec 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -458,13 +458,24 @@ }, { "cell_type": "code", - "execution_count": 29, - "id": "e62c1503", + "execution_count": 30, + "id": "267a4d22", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "WindowsPath('C:/Users/ifg/ED2')" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import pathlib\n", - "pathlib." + "pathlib.Path().resolve()" ] } ], From 85613f251b9488ff6d3696cdb733fdfca45a0c90 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 18 May 2022 08:42:06 -0300 Subject: [PATCH 05/12] remo --- .ipynb_checkpoints/ED2-checkpoint.ipynb | 582 ++++++++++++++++++++++++ ED2.ipynb | 225 ++++++--- 2 files changed, 750 insertions(+), 57 deletions(-) create mode 100644 .ipynb_checkpoints/ED2-checkpoint.ipynb diff --git a/.ipynb_checkpoints/ED2-checkpoint.ipynb b/.ipynb_checkpoints/ED2-checkpoint.ipynb new file mode 100644 index 0000000..39faabb --- /dev/null +++ b/.ipynb_checkpoints/ED2-checkpoint.ipynb @@ -0,0 +1,582 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 50, + "id": "d54522d8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from IPython.display import display, HTML\n", + "display(HTML(\"\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8b003e19", + "metadata": {}, + "outputs": [], + "source": [ + "class Vertice:\n", + " \n", + " def __init__(self, valor, direcionado=True):\n", + " self.__valor = valor\n", + " self.__direcionado = direcionado\n", + " self.__arestas = set()\n", + " \n", + " def getValor(self):\n", + " return self.__valor\n", + " \n", + " def setValor(self, valor):\n", + " self.__valor = valor\n", + " \n", + " def getArestas(self):\n", + " return self.__arestas\n", + " \n", + " def adicionarAresta(self, aresta):\n", + " self.__arestas.add(aresta)\n", + " \n", + " def getArestasSaida(self):\n", + " if self.__direcionado == False:\n", + " return self.__arestas\n", + " arestasDeSaida = []\n", + " for aresta in self.__arestas:\n", + " if aresta.getvOrigem() == self:\n", + " arestasDeSaida.append(aresta)\n", + " return arestasDeSaida\n", + " \n", + " def getArestasEntrada(self):\n", + " if self.__direcionado == False:\n", + " return self.__arestas\n", + " arestasSaida = []\n", + " for aresta in self.__arestas:\n", + " if aresta.getvDestino() == self:\n", + " arestasSaida.append(aresta)\n", + " return arestasSaida\n", + " \n", + " def getGrau(self):\n", + " return len(self.getArestasSaida())+ len(self.getArestasEntrada())\n", + " \n", + " def getAdjacentes(self, v):\n", + " listaVerticesAdjacentes = []\n", + " for arestas_de_saida in v.getArestasSaida():\n", + " listaVerticesAdjacentes.append(arestas_de_saida.getvDestino())\n", + " return listaVerticesAdjacentes" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5c3677e7", + "metadata": {}, + "outputs": [], + "source": [ + "class Aresta:\n", + " def __init__(self, vOrigem, vDestino, peso, direcionada=True):\n", + " self.__vOrigem = vOrigem\n", + " self.__vDestino = vDestino\n", + " self.__peso = peso\n", + " self.__direcionada = direcionada\n", + " self.__vOrigem.adicionarAresta(self)\n", + " self.__vDestino.adicionarAresta(self)\n", + " \n", + " def getvOrigem(self):\n", + " return self.__vOrigem\n", + " def getvDestino(self):\n", + " return self.__vDestino\n", + " def getValor(self):\n", + " return self.__peso" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d3d8fa70", + "metadata": {}, + "outputs": [], + "source": [ + "from collections import deque\n", + "class Grafo:\n", + " def __init__(self, direcionado=True):\n", + " self.__vertices = set()\n", + " self.__arestas = set()\n", + " self.__direcionado = direcionado\n", + " \n", + " def setVertices(self, vertices):\n", + " self.__vertices = vertices\n", + " \n", + " def setArestas(self, arestas):\n", + " self.__arestas = arestas\n", + " \n", + " def getVertices(self):\n", + " return self.__vertices\n", + " \n", + " def getVerticeByValor(self, valor):\n", + " for v in self.__vertices:\n", + " if v.getValor() == valor:\n", + " return v\n", + " return None\n", + " \n", + " def getArestas(self):\n", + " return self.__arestas\n", + " \n", + " def adicionarVertice(self, valor):\n", + " if self.buscarPorValor(valor) != valor:\n", + " self.__vertices.add(Vertice(valor))\n", + " return True\n", + " return False\n", + " \n", + " def adicionarAresta(self, origem, destino, peso = 1, direcionada = True):\n", + " try:\n", + " verticeOrigem = self.getVerticeByValor(origem)\n", + " verticeDestino = self.getVerticeByValor(destino)\n", + " if (verticeOrigem or verticeDestino) is None: \n", + " print(\"Nao ha no grafo, vertices de origem ou de destino com os valores informados.\")\n", + " self.__arestas.add(Aresta(verticeOrigem, verticeDestino, peso, direcionada))\n", + " except AttributeError as error:\n", + " print(\"Nao ha no grafo, vertices de origem ou de destino com os valores informados.\")\n", + " \n", + "\n", + " \n", + " def checkHandShakingLemma(self):\n", + " somaGraus = 0\n", + " for v in self.getVertices():\n", + " somaGraus+= v.getGrau()\n", + " if somaGraus == len(self.getArestas())*2:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " def dfs(self, graph, v, visitados=[]):\n", + " if v not in visitados:\n", + " visitados.append(v) \n", + " if len(v.getAdjacentes(v)) == 0: \n", + " self.dfs(graph, next(iter(graph.getVertices())), visitados) \n", + " else: \n", + " for adjacente in v.getAdjacentes(v):\n", + " if adjacente not in visitados:\n", + " self.dfs(graph, adjacente, visitados) \n", + " return visitados\n", + " \n", + " def bfs(self, v, visitados = [], fila = deque([])):\n", + " fila.append(v) \n", + " if v not in visitados: \n", + " visitados.append(v) \n", + " while fila: \n", + " vertice = fila.popleft() \n", + " if len(vertice.getArestasSaida()) == 0:\n", + " self.bfs(next(iter(self.getVertices())), visitados, fila) \n", + " else:\n", + " for adjacente in vertice.getAdjacentes(v): \n", + " if adjacente not in visitados:\n", + " visitados.append(adjacente) \n", + " self.bfs(adjacente, visitados, fila) \n", + " return visitados \n", + " \n", + " def buscarPorValor(self, valor):\n", + " for v in self.bfs(next(iter(self.getVertices())), visitados = [], fila = deque([])):\n", + " if valor == v.getValor():\n", + " return valor\n", + " return None\n", + " \n", + " def delverte(self):\n", + " \n", + " \n", + " for i in range(self, origem, destino, peso = 1, direcionada = True):\n", + " verticeOrigem = self.getVerticeByValor(origem)\n", + " verticeDestino = self.getVerticeByValor(destino)\n", + " if (verticeOrigem or verticeDestino) is None:\n", + " self.__arestas(Aresta(verticeOrigem, verticeDestino, peso, direcionada))\n", + " \n", + " \n", + " def removerVertice(self, vertice):\n", + " listaArestas = []\n", + " vertice = self.getVerticeByValor(vertice)\n", + " if vertice in self.getVertices():\n", + " for a in self.getArestas():\n", + " if (a.getvOrigem() == vertice) or (a.getvDestino() == vertice):\n", + " listaArestas.append(a)\n", + " for a in listaArestas:\n", + " self.__arestas.remove(a)\n", + " self.getVertices().remove(vertice)\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " def removerAresta(self, origem, destino, peso, direcionada=True):\n", + " Origem = self.getVerticeByValor(origem)\n", + " Destino = self.getVerticeByValor(destino)\n", + " remover_lista = list()\n", + " if (Destino or Origem) is not None: \n", + " for a in self.getArestas():\n", + " if (a.getvOrigem() == Origem) and (a.getvDestino() == Destino) and a.getValor() == peso:\n", + " remover_lista.append(a)\n", + " [self.getArestas().remove(a) for a in remover_lista]\n", + " return True\n", + " return False\n", + " \n", + " def Euler(self, graph):\n", + " impar = 0\n", + " for v in graph.getVertices():\n", + " if v.getGrau() %2 != 0:\n", + " impar += 1\n", + " if impar != 0 >= 2:\n", + " return True \n", + " else:\n", + " return False\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "91b986a7", + "metadata": {}, + "outputs": [], + "source": [ + "v1 = Vertice(1)\n", + "v2 = Vertice(2)\n", + "v3 = Vertice(3)\n", + "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", + "a1 = Aresta( v1, v2, 10, True )\n", + "a2 = Aresta( v2, v3, 20, True )\n", + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True )" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2db17f8a", + "metadata": {}, + "outputs": [], + "source": [ + "G = Grafo()\n", + "G.setVertices({v1, v2, v3, v4, v5})\n", + "G.setArestas({a1, a2, a3, a4, a5})" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "13453065", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nao ha no grafo, vertices de origem ou de destino com os valores informados.\n" + ] + } + ], + "source": [ + "G.adicionarVertice(7)\n", + "G.adicionarAresta(5, 15, 10, True)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "28e99364", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\t5\t3\t1\t7\t2\t" + ] + } + ], + "source": [ + "for v in G.getVertices():\n", + " print(v.getValor(), end=\"\\t\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "29d5dcfe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 ---> 3\t1 ---> 2\t4 ---> 5\t3 ---> 4\t4 ---> 1\t" + ] + } + ], + "source": [ + "for a in G.getArestas():\n", + " print(a.getvOrigem().getValor(), end=\"\")\n", + " print(\" ---> \", end=\"\")\n", + " print(a.getvDestino().getValor(), end=\"\\t\")" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "3ebc1c5d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v1.getGrau()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "4690659b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "G.checkHandShakingLemma()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "1e0f4098", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Busca em profundidade, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t6\t5\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 5:\n", + "5\t1\t2\t3\t4\t6\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 4:\n", + "4\t1\t2\t3\t6\t5\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 2:\n", + "2\t3\t4\t1\t6\t5\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 3:\n", + "3\t4\t1\t2\t6\t5\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 6:\n", + "6\t1\t2\t3\t4\t5\t\n", + ".................................................\n" + ] + } + ], + "source": [ + "v1 = Vertice(1)\n", + "v2 = Vertice(2)\n", + "v3 = Vertice(3)\n", + "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", + "v6 = Vertice(6)\n", + "a1 = Aresta( v1, v2, 10, True )\n", + "a2 = Aresta( v2, v3, 20, True )\n", + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True ) \n", + "a6 = Aresta( v4, v6, 60, True ) \n", + "G = Grafo()\n", + "G.setVertices({v1, v2, v3, v4, v5,v6})\n", + "G.setArestas({a1, a2, a3, a4, a5, a6})\n", + "for vertice in G.getVertices():\n", + " print(f\"Busca em profundidade, iniciando com o vértice {vertice.getValor()}:\")\n", + " for v in G.dfs(G, vertice, visitados=[]):\n", + " print(str(v.getValor())+\"\\t\", end=\"\")\n", + " print(\"\\n.................................................\") " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b7459764", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Busca em largura, iniciando com o vértice 4:\n", + "4\t6\t5\t1\t2\t3\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t6\t5\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 2:\n", + "2\t3\t4\t6\t5\t1\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 6:\n", + "6\t4\t5\t1\t2\t3\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 3:\n", + "3\t4\t6\t5\t1\t2\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 5:\n", + "5\t4\t6\t1\t2\t3\t\n", + "............................................\n" + ] + } + ], + "source": [ + "v1 = Vertice(1)\n", + "v2 = Vertice(2)\n", + "v3 = Vertice(3)\n", + "v4 = Vertice(4)\n", + "v5 = Vertice(5)\n", + "v6 = Vertice(6)\n", + "a1 = Aresta( v1, v2, 10, True )\n", + "a2 = Aresta( v2, v3, 20, True )\n", + "a3 = Aresta( v3, v4, 30, True )\n", + "a4 = Aresta( v4, v1, 40, True )\n", + "a5 = Aresta( v4, v5, 50, True ) \n", + "a6 = Aresta( v4, v6, 60, True ) \n", + "G = Grafo()\n", + "G.setVertices({v1, v2, v3, v4, v5,v6})\n", + "G.setArestas({a1, a2, a3, a4, a5, a6})\n", + "for vertice in G.getVertices():\n", + " print(f\"Busca em largura, iniciando com o vértice {vertice.getValor()}:\")\n", + " for v in G.bfs(vertice, visitados = [], fila = deque([])):\n", + " print(str(v.getValor())+\"\\t\", end=\"\")\n", + " print(\"\\n............................................\") " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "b29de58e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "não é um grafo euleriano\n" + ] + } + ], + "source": [ + "if G.Euler(G):\n", + " print(\"é um grafo euleriano\")\n", + "else:\n", + " print(\"não é um grafo euleriano\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "30ca64af", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Valor procurado: 7\n", + "7 não encontrado no grafo\n" + ] + } + ], + "source": [ + "valor = int(input(\"Valor procurado: \"))\n", + "if G.buscarPorValor(valor) == None:\n", + " print(f\"{valor} não encontrado no grafo\")\n", + "else:\n", + " print(f\"{valor} encontrado no grafo\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "267a4d22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "WindowsPath('C:/Users/ifg/ED2')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pathlib\n", + "pathlib.Path().resolve()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c954d113", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/ED2.ipynb b/ED2.ipynb index 1f9f6ec..d3898f8 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 2, "id": "8b003e19", "metadata": {}, "outputs": [], @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 3, "id": "5c3677e7", "metadata": {}, "outputs": [], @@ -104,7 +104,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 4, "id": "d3d8fa70", "metadata": {}, "outputs": [], @@ -134,6 +134,24 @@ " def getArestas(self):\n", " return self.__arestas\n", " \n", + " def adicionarVertice(self, valor):\n", + " if self.buscarPorValor(valor) != valor:\n", + " self.__vertices.add(Vertice(valor))\n", + " return True\n", + " return False\n", + " \n", + " def adicionarAresta(self, origem, destino, peso = 1, direcionada = True):\n", + " try:\n", + " verticeOrigem = self.getVerticeByValor(origem)\n", + " verticeDestino = self.getVerticeByValor(destino)\n", + " if (verticeOrigem or verticeDestino) is None: \n", + " print(\"Nao ha no grafo, vertices de origem ou de destino com os valores informados.\")\n", + " self.__arestas.add(Aresta(verticeOrigem, verticeDestino, peso, direcionada))\n", + " except AttributeError as error:\n", + " print(\"Nao ha no grafo, vertices de origem ou de destino com os valores informados.\")\n", + " \n", + "\n", + " \n", " def checkHandShakingLemma(self):\n", " somaGraus = 0\n", " for v in self.getVertices():\n", @@ -146,29 +164,28 @@ " def dfs(self, graph, v, visitados=[]):\n", " if v not in visitados:\n", " visitados.append(v) \n", - " if len(v.getAdjacentes(v)) == 0:\n", - " self.dfs(graph, next(iter(graph.getVertices())), visitados)\n", - " else: # vertice escolhido tem adjacentes\n", - " for adjacente in v.getAdjacentes(v): \n", - " if adjacente not in visitados: \n", + " if len(v.getAdjacentes(v)) == 0: \n", + " self.dfs(graph, next(iter(graph.getVertices())), visitados) \n", + " else: \n", + " for adjacente in v.getAdjacentes(v):\n", + " if adjacente not in visitados:\n", " self.dfs(graph, adjacente, visitados) \n", " return visitados\n", " \n", " def bfs(self, v, visitados = [], fila = deque([])):\n", " fila.append(v) \n", " if v not in visitados: \n", - " visitados.append(v)\n", + " visitados.append(v) \n", " while fila: \n", " vertice = fila.popleft() \n", - " if len(vertice.getArestasSaida()) == 0: \n", - " self.bfs(next(iter(self.getVertices())), visitados, fila) \n", + " if len(vertice.getArestasSaida()) == 0:\n", + " self.bfs(next(iter(self.getVertices())), visitados, fila) \n", " else:\n", - " for e in vertice.getArestasSaida(): \n", - " vAdjacente = e.getvDestino()\n", - " if vAdjacente not in visitados: \n", - " visitados.append(vAdjacente) \n", - " fila.append(vAdjacente) \n", - " return visitados\n", + " for adjacente in vertice.getAdjacentes(v): \n", + " if adjacente not in visitados:\n", + " visitados.append(adjacente) \n", + " self.bfs(adjacente, visitados, fila) \n", + " return visitados \n", " \n", " def buscarPorValor(self, valor):\n", " for v in self.bfs(next(iter(self.getVertices())), visitados = [], fila = deque([])):\n", @@ -176,6 +193,40 @@ " return valor\n", " return None\n", " \n", + " def delverte(self):\n", + " for i in range(self, origem, destino, peso = 1, direcionada = True):\n", + " verticeOrigem = self.getVerticeByValor(origem)\n", + " verticeDestino = self.getVerticeByValor(destino)\n", + " if (verticeOrigem or verticeDestino) is None:\n", + " self.__arestas(Aresta(verticeOrigem, verticeDestino, peso, direcionada))\n", + " \n", + " \n", + " def removerVertice(self, vertice):\n", + " listaArestas = []\n", + " vertice = self.getVerticeByValor(vertice)\n", + " if vertice in self.getVertices():\n", + " for a in self.getArestas():\n", + " if (a.getvOrigem() == vertice) or (a.getvDestino() == vertice):\n", + " listaArestas.append(a)\n", + " for a in listaArestas:\n", + " self.__arestas.remove(a)\n", + " self.getVertices().remove(vertice)\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + " def removerAresta(self, origem, destino, peso, direcionada=True):\n", + " Origem = self.getVerticeByValor(origem)\n", + " Destino = self.getVerticeByValor(destino)\n", + " remover_lista = list()\n", + " if (Destino or Origem) is not None: \n", + " for a in self.getArestas():\n", + " if (a.getvOrigem() == Origem) and (a.getvDestino() == Destino) and a.getValor() == peso:\n", + " remover_lista.append(a)\n", + " [self.getArestas().remove(a) for a in remover_lista]\n", + " return True\n", + " return False\n", + "\n", " def Euler(self, graph):\n", " impar = 0\n", " for v in graph.getVertices():\n", @@ -185,13 +236,12 @@ " return True \n", " else:\n", " return False\n", - "\n", " " ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 5, "id": "91b986a7", "metadata": {}, "outputs": [], @@ -210,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 6, "id": "2db17f8a", "metadata": {}, "outputs": [], @@ -222,7 +272,26 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 7, + "id": "13453065", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nao ha no grafo, vertices de origem ou de destino com os valores informados.\n" + ] + } + ], + "source": [ + "G.adicionarVertice(7)\n", + "G.adicionarAresta(5, 15, 10, True)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, "id": "28e99364", "metadata": {}, "outputs": [ @@ -230,7 +299,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "5\t3\t4\t1\t2\t" + "3\t5\t7\t1\t4\t2\t" ] } ], @@ -241,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 9, "id": "29d5dcfe", "metadata": {}, "outputs": [ @@ -249,7 +318,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "1 ---> 2\t4 ---> 5\t2 ---> 3\t4 ---> 1\t3 ---> 4\t" + "4 ---> 1\t2 ---> 3\t1 ---> 2\t4 ---> 5\t3 ---> 4\t" ] } ], @@ -262,7 +331,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 10, "id": "3ebc1c5d", "metadata": {}, "outputs": [ @@ -272,7 +341,7 @@ "2" ] }, - "execution_count": 23, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -283,17 +352,17 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 11, "id": "4690659b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "False" ] }, - "execution_count": 24, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -304,7 +373,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 12, "id": "1e0f4098", "metadata": {}, "outputs": [ @@ -312,23 +381,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "Busca em profundidade, iniciando com o vértice 2:\n", - "2\t3\t4\t5\t6\t1\t\n", + "Busca em profundidade, iniciando com o vértice 4:\n", + "4\t6\t1\t2\t3\t5\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 5:\n", - "5\t2\t3\t4\t6\t1\t\n", + "Busca em profundidade, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t6\t5\t\n", ".................................................\n", "Busca em profundidade, iniciando com o vértice 3:\n", - "3\t4\t5\t2\t6\t1\t\n", + "3\t4\t6\t1\t2\t5\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 6:\n", - "6\t2\t3\t4\t5\t1\t\n", + "Busca em profundidade, iniciando com o vértice 5:\n", + "5\t4\t6\t1\t2\t3\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 4:\n", - "4\t5\t2\t3\t6\t1\t\n", + "Busca em profundidade, iniciando com o vértice 6:\n", + "6\t4\t1\t2\t3\t5\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 1:\n", - "1\t2\t3\t4\t5\t6\t\n", + "Busca em profundidade, iniciando com o vértice 2:\n", + "2\t3\t4\t6\t1\t5\t\n", ".................................................\n" ] } @@ -358,7 +427,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 13, "id": "b7459764", "metadata": { "scrolled": true @@ -368,23 +437,23 @@ "name": "stdout", "output_type": "stream", "text": [ + "Busca em largura, iniciando com o vértice 3:\n", + "3\t4\t1\t2\t5\t6\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 5:\n", + "5\t3\t4\t1\t2\t6\t\n", + "............................................\n", "Busca em largura, iniciando com o vértice 1:\n", "1\t2\t3\t4\t5\t6\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 4:\n", - "4\t5\t6\t1\t2\t3\t\n", - "............................................\n", - "Busca em largura, iniciando com o vértice 3:\n", - "3\t4\t5\t6\t1\t2\t\n", + "4\t1\t2\t3\t5\t6\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 2:\n", - "2\t3\t4\t5\t6\t1\t\n", - "............................................\n", - "Busca em largura, iniciando com o vértice 5:\n", - "5\t1\t2\t3\t4\t6\t\n", + "2\t3\t4\t1\t5\t6\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 6:\n", - "6\t1\t2\t3\t4\t5\t\n", + "6\t3\t4\t1\t2\t5\t\n", "............................................\n" ] } @@ -414,7 +483,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 14, "id": "b29de58e", "metadata": {}, "outputs": [ @@ -435,7 +504,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 15, "id": "30ca64af", "metadata": {}, "outputs": [ @@ -443,8 +512,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Valor procurado: 7\n", - "7 não encontrado no grafo\n" + "Valor procurado: 1\n", + "1 encontrado no grafo\n" ] } ], @@ -458,7 +527,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 16, "id": "267a4d22", "metadata": {}, "outputs": [ @@ -468,7 +537,7 @@ "WindowsPath('C:/Users/ifg/ED2')" ] }, - "execution_count": 30, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -477,6 +546,48 @@ "import pathlib\n", "pathlib.Path().resolve()" ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "7aba71ef", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "G.removerVertice(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c954d113", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "G.removerAresta(1, 2, True)" + ] } ], "metadata": { From 752db69b496c7e2a1fef1f0a3f9560b3b8552253 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 25 May 2022 11:31:51 -0300 Subject: [PATCH 06/12] Dijkstra --- ED2.ipynb | 197 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 170 insertions(+), 27 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index d3898f8..925626a 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -226,6 +226,7 @@ " [self.getArestas().remove(a) for a in remover_lista]\n", " return True\n", " return False\n", + " \n", "\n", " def Euler(self, graph):\n", " impar = 0\n", @@ -235,8 +236,7 @@ " if impar != 0 >= 2:\n", " return True \n", " else:\n", - " return False\n", - " " + " return False" ] }, { @@ -299,7 +299,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "3\t5\t7\t1\t4\t2\t" + "3\t2\t1\t5\t4\t7\t" ] } ], @@ -318,7 +318,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "4 ---> 1\t2 ---> 3\t1 ---> 2\t4 ---> 5\t3 ---> 4\t" + "4 ---> 1\t3 ---> 4\t1 ---> 2\t4 ---> 5\t2 ---> 3\t" ] } ], @@ -381,23 +381,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "Busca em profundidade, iniciando com o vértice 4:\n", - "4\t6\t1\t2\t3\t5\t\n", + "Busca em profundidade, iniciando com o vértice 2:\n", + "2\t3\t4\t6\t1\t5\t\n", ".................................................\n", "Busca em profundidade, iniciando com o vértice 1:\n", "1\t2\t3\t4\t6\t5\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 3:\n", - "3\t4\t6\t1\t2\t5\t\n", - ".................................................\n", "Busca em profundidade, iniciando com o vértice 5:\n", - "5\t4\t6\t1\t2\t3\t\n", + "5\t2\t3\t4\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 4:\n", + "4\t6\t2\t3\t1\t5\t\n", ".................................................\n", "Busca em profundidade, iniciando com o vértice 6:\n", - "6\t4\t1\t2\t3\t5\t\n", + "6\t2\t3\t4\t1\t5\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 2:\n", - "2\t3\t4\t6\t1\t5\t\n", + "Busca em profundidade, iniciando com o vértice 3:\n", + "3\t4\t6\t2\t1\t5\t\n", ".................................................\n" ] } @@ -437,23 +437,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "Busca em largura, iniciando com o vértice 3:\n", - "3\t4\t1\t2\t5\t6\t\n", - "............................................\n", - "Busca em largura, iniciando com o vértice 5:\n", - "5\t3\t4\t1\t2\t6\t\n", + "Busca em largura, iniciando com o vértice 2:\n", + "2\t3\t4\t6\t1\t5\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 1:\n", - "1\t2\t3\t4\t5\t6\t\n", + "1\t2\t3\t4\t6\t5\t\n", "............................................\n", - "Busca em largura, iniciando com o vértice 4:\n", - "4\t1\t2\t3\t5\t6\t\n", + "Busca em largura, iniciando com o vértice 3:\n", + "3\t4\t6\t2\t1\t5\t\n", "............................................\n", - "Busca em largura, iniciando com o vértice 2:\n", - "2\t3\t4\t1\t5\t6\t\n", + "Busca em largura, iniciando com o vértice 5:\n", + "5\t2\t3\t4\t6\t1\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 6:\n", - "6\t3\t4\t1\t2\t5\t\n", + "6\t2\t3\t4\t1\t5\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 4:\n", + "4\t6\t2\t3\t1\t5\t\n", "............................................\n" ] } @@ -512,8 +512,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Valor procurado: 1\n", - "1 encontrado no grafo\n" + "Valor procurado: 2\n", + "2 encontrado no grafo\n" ] } ], @@ -550,7 +550,7 @@ { "cell_type": "code", "execution_count": 17, - "id": "7aba71ef", + "id": "1dd0997c", "metadata": {}, "outputs": [ { @@ -588,6 +588,149 @@ "source": [ "G.removerAresta(1, 2, True)" ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "cdff3f83", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1: 2 -> 3 -> 4 -> \n", + "2: 3 -> \n", + "3: \n", + "4: \n" + ] + } + ], + "source": [ + "class Grafo:\n", + "\n", + " def __init__(self, vertices):\n", + " self.vertices = vertices\n", + " self.grafo = [[] for i in range(self.vertices)]\n", + "\n", + " def adicionaAresta(self, u, v):\n", + " self.grafo[u-1].append(v)\n", + "\n", + "\n", + " def mostraLista(self):\n", + " for i in range(self.vertices):\n", + " print(f'{i+1}:', end=' ')\n", + " for j in self.grafo[i]:\n", + " print(f'{j} ->', end=' ')\n", + " print('')\n", + "\n", + "g = Grafo(4)\n", + "\n", + "g.adicionaAresta(1,2)\n", + "g.adicionaAresta(1,3)\n", + "g.adicionaAresta(1,4)\n", + "g.adicionaAresta(2,3)\n", + "\n", + "g.mostraLista()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "afdafed7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 0.8, 0.3, 1.1, 2.3, 0.2]\n" + ] + } + ], + "source": [ + "import heapq\n", + "\n", + "\n", + "class Grafo():\n", + "\n", + " def __init__(self, arestas: list):\n", + " self.adj = [[] for _ in range(len(arestas))]\n", + " self.dist = [999 for _ in range(len(arestas))]\n", + " self.adiciona_arestas(arestas)\n", + "\n", + " def adiciona_arestas(self, arestas: list) -> None:\n", + " for i in range(len(arestas)):\n", + " for j in range(len(arestas[i])):\n", + " self.__adiciona_aresta(i, arestas[i][j])\n", + "\n", + " def __adiciona_aresta(self, u: int, v: int) -> None:\n", + " if v[0] not in self.adj[u]:\n", + " self.adj[u].append([v[1], v[0]])\n", + "\n", + " def _peso_entre_u_e_v(self, u: int, v: int) -> float:\n", + " for vertice in self.adj[v[1]]:\n", + " if vertice[1] == u:\n", + " return vertice[0]\n", + "\n", + " def dijkstra(self, start: int) -> list:\n", + " distancia = self.dist.copy()\n", + " S = set() \n", + " distancia[start] = 0\n", + "\n", + " while True:\n", + " V = set([(i, distancia[i]) for i in range(len(self.adj))])\n", + " diferenca_de_conjuntos = list(V.difference(S))\n", + " if not diferenca_de_conjuntos:\n", + " break\n", + "\n", + " heapq.heapify(diferenca_de_conjuntos)\n", + " u, distancia_u = heapq.heappop(diferenca_de_conjuntos)\n", + "\n", + " S.add((u, distancia[u]))\n", + " for v in self.adj[u]:\n", + " if distancia[v[1]] > distancia_u + self._peso_entre_u_e_v(u, v):\n", + " distancia[v[1]] = distancia_u + \\\n", + " self._peso_entre_u_e_v(u, v)\n", + "\n", + " return distancia\n", + "\n", + "\n", + "arestas = [[[1, 2], [2, 0.3], [5, 0.4]], \n", + " [[0, 1], [2, 0.5]], \n", + " [[0, 0.3], [1, 0.5], [3, 1.2], [4, 1]], \n", + " [[2, 1.5], [4, 1.2], [5, 0.9]], \n", + " [[2, 2], [3, 1.5]], \n", + " [[0, 0.2], [3, 0.]]\n", + " ]\n", + "\n", + "grafo = Grafo(arestas)\n", + "print(grafo.dijkstra(0))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c980535b", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ebe593cf", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c1daf09", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { From c2db50cd11c6a8dca889c18045ad16907e87ba8f Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 6 Jul 2022 07:46:56 -0300 Subject: [PATCH 07/12] Dijkstra --- ED2.ipynb | 146 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 108 insertions(+), 38 deletions(-) diff --git a/ED2.ipynb b/ED2.ipynb index 925626a..8b867be 100644 --- a/ED2.ipynb +++ b/ED2.ipynb @@ -299,7 +299,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "3\t2\t1\t5\t4\t7\t" + "1\t4\t2\t3\t7\t5\t" ] } ], @@ -318,7 +318,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "4 ---> 1\t3 ---> 4\t1 ---> 2\t4 ---> 5\t2 ---> 3\t" + "4 ---> 1\t4 ---> 5\t1 ---> 2\t2 ---> 3\t3 ---> 4\t" ] } ], @@ -382,22 +382,22 @@ "output_type": "stream", "text": [ "Busca em profundidade, iniciando com o vértice 2:\n", - "2\t3\t4\t6\t1\t5\t\n", + "2\t3\t4\t5\t6\t1\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 1:\n", - "1\t2\t3\t4\t6\t5\t\n", - ".................................................\n", - "Busca em profundidade, iniciando com o vértice 5:\n", - "5\t2\t3\t4\t6\t1\t\n", + "Busca em profundidade, iniciando com o vértice 3:\n", + "3\t4\t5\t2\t6\t1\t\n", ".................................................\n", "Busca em profundidade, iniciando com o vértice 4:\n", - "4\t6\t2\t3\t1\t5\t\n", + "4\t5\t2\t3\t6\t1\t\n", ".................................................\n", "Busca em profundidade, iniciando com o vértice 6:\n", - "6\t2\t3\t4\t1\t5\t\n", + "6\t2\t3\t4\t5\t1\t\n", ".................................................\n", - "Busca em profundidade, iniciando com o vértice 3:\n", - "3\t4\t6\t2\t1\t5\t\n", + "Busca em profundidade, iniciando com o vértice 5:\n", + "5\t2\t3\t4\t6\t1\t\n", + ".................................................\n", + "Busca em profundidade, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t5\t6\t\n", ".................................................\n" ] } @@ -437,23 +437,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "Busca em largura, iniciando com o vértice 2:\n", - "2\t3\t4\t6\t1\t5\t\n", + "Busca em largura, iniciando com o vértice 3:\n", + "3\t4\t1\t2\t5\t6\t\n", "............................................\n", - "Busca em largura, iniciando com o vértice 1:\n", - "1\t2\t3\t4\t6\t5\t\n", + "Busca em largura, iniciando com o vértice 6:\n", + "6\t3\t4\t1\t2\t5\t\n", "............................................\n", - "Busca em largura, iniciando com o vértice 3:\n", - "3\t4\t6\t2\t1\t5\t\n", + "Busca em largura, iniciando com o vértice 2:\n", + "2\t3\t4\t1\t5\t6\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 5:\n", - "5\t2\t3\t4\t6\t1\t\n", - "............................................\n", - "Busca em largura, iniciando com o vértice 6:\n", - "6\t2\t3\t4\t1\t5\t\n", + "5\t3\t4\t1\t2\t6\t\n", "............................................\n", "Busca em largura, iniciando com o vértice 4:\n", - "4\t6\t2\t3\t1\t5\t\n", + "4\t1\t2\t3\t5\t6\t\n", + "............................................\n", + "Busca em largura, iniciando com o vértice 1:\n", + "1\t2\t3\t4\t5\t6\t\n", "............................................\n" ] } @@ -512,8 +512,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Valor procurado: 2\n", - "2 encontrado no grafo\n" + "Valor procurado: 4\n", + "4 encontrado no grafo\n" ] } ], @@ -636,7 +636,66 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 20, + "id": "d3c0b8e0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 0, 'b': -1, 'c': 3, 'd': -2, 'e': 1}\n", + "{'a': 0, 'b': 10, 'c': 2, 'd': 3}\n" + ] + } + ], + "source": [ + "def bellman_ford(graph, source):\n", + " distancia, antecessor = dict(), dict()\n", + " for node in graph:\n", + " distancia[node], antecessor[node] = float('inf'), None\n", + " distancia[source] = 0\n", + "\n", + " for _ in range(len(graph) - 1):\n", + " for node in graph:\n", + " for neighbour in graph[node]:\n", + " if distancia[neighbour] > distancia[node] + graph[node][neighbour]:\n", + " distancia[neighbour], antecessor[neighbour] = distancia[node] + graph[node][neighbour], node\n", + "\n", + " for node in graph:\n", + " for neighbour in graph[node]:\n", + " assert distancia[neighbour] <= distancia[node] + graph[node][neighbour], \"Negativo ciclo.\"\n", + " \n", + " return distancia, antecessor\n", + " \n", + "if __name__ == '__main__':\n", + " graph = {\n", + " 'a': {'b': -1, 'c': 4},\n", + " 'b': {'c': 5, 'd': 3, 'e': 2},\n", + " 'c': {},\n", + " 'd': {'b': 2, 'c': 5},\n", + " 'e': {'d': -3}\n", + " }\n", + "\n", + " distancia, antecessor = bellman_ford(graph, source='a')\n", + "\n", + " print (distancia)\n", + " \n", + " graph = {\n", + " 'a': {'c': 2},\n", + " 'b': {'a': 2},\n", + " 'c': {'b': 8, 'd': 1},\n", + " 'd': {'a': 6},\n", + " }\n", + " \n", + " distancia, antecessor = bellman_ford(graph, source='a')\n", + "\n", + " print (distancia)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, "id": "afdafed7", "metadata": {}, "outputs": [ @@ -710,24 +769,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "c980535b", "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ebe593cf", - "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "moedas = [100, 50, 25, 5, 1]\n", + "total = 0\n", + "troco = 130\n", + "\n", + "for i in range(len(moedas)):\n", + " num_moedas = troco // moedas[i]\n", + " troco -= num_moedas * moedas[i]\n", + " total += num_moedas\n", + "\n", + "print(total)" + ] }, { "cell_type": "code", "execution_count": null, - "id": "1c1daf09", + "id": "da4ee43a", "metadata": {}, "outputs": [], "source": [] From 9350a627376bbc9bc6545335e902c89a1fed786e Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 13 Jul 2022 07:36:08 -0300 Subject: [PATCH 08/12] Prim --- prim.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 prim.txt diff --git a/prim.txt b/prim.txt new file mode 100644 index 0000000..9e9a26e --- /dev/null +++ b/prim.txt @@ -0,0 +1,31 @@ +INF = 9999999 +N = 5 +G = [[0, 19, 5, 0, 0], + [19, 0, 5, 9, 2], + [5, 5, 0, 1, 6], + [0, 9, 1, 0, 1], + [0, 2, 6, 1, 0]] + +seceleciona_node = [0, 0, 0, 0, 0] + +no_edge = 0 + +seceleciona_node[0] = True + +print("Edge : Weight\n") +while (no_edge < N - 1): + + minimum = INF + a = 0 + b = 0 + for m in range(N): + if seceleciona_node[m]: + for n in range(N): + if ((not seceleciona_node[n]) and G[m][n]): + if minimum > G[m][n]: + minimum = G[m][n] + a = m + b = n + print(str(a) + "-" + str(b) + ":" + str(G[a][b])) + seceleciona_node[b] = True + no_edge += 1 \ No newline at end of file From 9f67ef9aacf5a02a896cf50eeaa336b9f078e8a9 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 13 Jul 2022 07:36:44 -0300 Subject: [PATCH 09/12] Kruskal --- kruskal.txt | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 kruskal.txt diff --git a/kruskal.txt b/kruskal.txt new file mode 100644 index 0000000..147fd7b --- /dev/null +++ b/kruskal.txt @@ -0,0 +1,64 @@ +class Graph: + def __init__(self, vertices): + self.V = vertices + self.graph = [] + + def add_edge(self, u, v, w): + self.graph.append([u, v, w]) + + + def find(self, parente, i): + if parente[i] == i: + return i + return self.find(parente, parente[i]) + + def apply_union(self, parente, rank, x, y): + xroot = self.find(parente, x) + yroot = self.find(parente, y) + if rank[xroot] < rank[yroot]: + parente[xroot] = yroot + elif rank[xroot] > rank[yroot]: + parente[yroot] = xroot + else: + parente[yroot] = xroot + rank[xroot] += 1 + + def kruskal_algo(self): + result = [] + i, e = 0, 0 + self.graph = sorted(self.graph, key=lambda item: item[2]) + parente = [] + rank = [] + for node in range(self.V): + parente.append(node) + rank.append(0) + while e < self.V - 1: + u, v, w = self.graph[i] + i = i + 1 + x = self.find(parente, u) + y = self.find(parente, v) + if x != y: + e = e + 1 + result.append([u, v, w]) + self.apply_union(parente, rank, x, y) + for u, v, weight in result: + print("%d - %d: %d" % (u, v, weight)) + + +g = Graph(6) +g.add_edge(0, 1, 4) +g.add_edge(0, 2, 4) +g.add_edge(1, 2, 2) +g.add_edge(1, 0, 4) +g.add_edge(2, 0, 4) +g.add_edge(2, 1, 2) +g.add_edge(2, 3, 3) +g.add_edge(2, 5, 2) +g.add_edge(2, 4, 4) +g.add_edge(3, 2, 3) +g.add_edge(3, 4, 3) +g.add_edge(4, 2, 4) +g.add_edge(4, 3, 3) +g.add_edge(5, 2, 2) +g.add_edge(5, 4, 3) +g.kruskal_algo() \ No newline at end of file From 6a7802dd1d654358684ec0d59ff0ce01400b3631 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 13 Jul 2022 07:37:09 -0300 Subject: [PATCH 10/12] Mochila --- Mochila.txt | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Mochila.txt diff --git a/Mochila.txt b/Mochila.txt new file mode 100644 index 0000000..a78740f --- /dev/null +++ b/Mochila.txt @@ -0,0 +1,39 @@ +class Mochila: + def __init__(self, peso_objeto = 20): + self.__peso_objeto = peso_objeto + self.__itens_adicionados = list() + + + def adicionar_itens(self, objetos = list()): + peso_disponivel = self.__peso_objeto + pos = 0 + objetos.sort(key = lambda i:i.getM(), reverse = True) + while peso_disponivel != 0: + if objetos[pos].getPeso() <= peso_disponivel: + self.__itens_adicionados.append(objetos[pos]) + peso_disponivel -= objetos[pos].getPeso() + else: + pos += 1 + + def getItems(self): + return self.__itens_adicionados +class Item: + def __init__(self, nome_item, valor_item, peso_item): + self.__nome_item = nome_item + self.__valor_item = valor_item + self.__peso_item = peso_item + self.__m = valor_item / peso_item + + def getM(self): + return self.__m + + def getPeso(self): + return self.__peso_item + + def getNome(self): + return self.__nome_item +M = Mochila() +M.adicionar_itens([Item("Ouro", 100, 5), Item("Prata", 20, 2), Item("Diamante", 200, 7), Item("Ferro", 5, 1),]) + +for i in M.getItems(): + print(i.getNome()) \ No newline at end of file From c308da141ac73fee920ea6e732c94b304495bf02 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 13 Jul 2022 07:38:47 -0300 Subject: [PATCH 11/12] Cedulas --- Cedulas.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Cedulas.txt diff --git a/Cedulas.txt b/Cedulas.txt new file mode 100644 index 0000000..21bd7ed --- /dev/null +++ b/Cedulas.txt @@ -0,0 +1,10 @@ +moedas = [100, 50, 25, 5, 1] +total = 0 +notas = 130 + +for i in range(len(moedas)): + num_moedas = notas // moedas[i] + notas -= num_moedas * moedas[i] + total += num_moedas + +print(total) \ No newline at end of file From a188501e8ae30672976e9a037e038cf4fb907a83 Mon Sep 17 00:00:00 2001 From: karllosDR <49981663+karllosDR@users.noreply.github.com> Date: Wed, 20 Jul 2022 08:47:51 -0300 Subject: [PATCH 12/12] Floyd Warshall --- Floyd Warshall.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Floyd Warshall.py diff --git a/Floyd Warshall.py b/Floyd Warshall.py new file mode 100644 index 0000000..0769b7a --- /dev/null +++ b/Floyd Warshall.py @@ -0,0 +1,37 @@ +# Algoritmo Floyd Warshall em python + +# O número de vértices +nV = 4 + +INF = 999 + + +# Implementação do algoritmo +def floyd_warshall(G): + distancia = list(map(lambda i: list(map(lambda j: j, i)), G)) + + # Adiciona vértices individualmente + for k in range(nV): + for i in range(nV): + for j in range(nV): + distancia[i][j] = min(distancia[i][j], distancia[i][k] + distancia[k][j]) + print_solucao(distancia) + + +# Imprimindo a solução +def print_solucao(distancia): + for i in range(nV): + for j in range(nV): + if(distancia[i][j] == INF): + print("INF", end=" ") + else: + print(distancia[i][j], end=" ") + print(" ") + + +G = [[0, 3, 4, INF], + [INF, 0, INF, 5], + [INF, INF, 0, 3], + [8, INF, INF, 0]] + +floyd_warshall(G) \ No newline at end of file