Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
first commit
  • Loading branch information
Aruuthakur committed Oct 5, 2023
commit 25be4e73852f08c2ab93282482b1ea8d5a6cee6c
2 changes: 1 addition & 1 deletion src/python/arvore_binaria_de_busca.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Arvore Binaria de Busca em Python
Binary Search Tree in Python
Kelvin Salton do Prado
2015
"""
Expand Down
2 changes: 1 addition & 1 deletion src/python/binary_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Implementação de uma árvore binária """
""" Implementation of a binary tree """


class Node:
Expand Down
13 changes: 6 additions & 7 deletions src/python/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
""" Implementação do algoritmo bubble sort com recursão """

""" Implementation of the bubble sort algorithm with recursion """

def bubble_sort(data, size):
"""
Implementação de um algoritmo de bubble sort com recursão.
Implementation of a bubble sort algorithm with recursion.

Argumentos:
data: lista. Lista que será ordenada
size: int. Tamanho da lista
Arguments:
date: list. List to be sorted
size: int. List size

Retorna a lista "data" ordenada.
Returns the ordered "date" list.
"""
swap = False
for i in range(0, size - 1):
Expand Down
40 changes: 20 additions & 20 deletions src/python/busca_binaria.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
""" Implementação do algoritmo de busca binária com recursão """
""" Implementation of the binary search algorithm with recursion """


def busca_binaria(valor, vetor, esquerda, direita):
def binary_search(value, vector, left, right):
"""
Implementação de um algoritmo de busca binária com recursão.
Implementation of a binary search algorithm with recursion.

Argumentos:
valor: Any. Valor a ser buscado na lista
vetor: list. lista ordenada na qual o valor será buscado
esquerda: Any. Valor inicial da metade buscada
direita: Any. Valor final da metade buscada
Arguments:
value: Any. Value to be searched for in the list
vector: list. ordered list in which the value will be searched
left: Any. Initial value of the half sought
right: Any. Final value of the half sought

Retorna o índice do valor em "vetor" ou -1 caso não exista nela.
Returns the index of the value in "vector" or -1 if it does not exist in it.
"""
meio = int((esquerda + direita) / 2)
middle = int((left + right) / 2)

if esquerda <= direita:
if valor > vetor[meio]:
esquerda = meio + 1
return busca_binaria(valor, vetor, esquerda, direita)
elif valor < vetor[meio]:
direita = meio - 1
return busca_binaria(valor, vetor, esquerda, direita)
return meio
if left <= right:
if value > vector[middle]:
left = middle + 1
return binary_search(value, vector, left, right)
elif value < vector[middle]:
right = middle - 1
return binary_search(value, vector, left, right)
return middle
return -1


lista = [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12]
print(busca_binaria(12, lista, 0, len(lista) - 1))
list = [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12]
print(binary_search(12, list, 0, len(list) - 1))
110 changes: 55 additions & 55 deletions src/python/busca_em_grafo.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# Grafos - Algoritmos de BFS e DFS em Python
# Graphs - BFS and DFS Algorithms in Python
# Bruno Dantas de Paiva - 2021
# https://github.com/DantasB

from collections import deque


class Grafo:
"""Define um grafo utilizando matriz de adjacências.
class Graph:
"""Defines a graph using an adjacency matrix.

Args:
arestas (list): uma lista de listas onde o indice é o
vértice e cada elemento da lista é o vizinho
edges (list): A list of lists where the index is the
vertex, and each element of the list is a neighbor.
"""

def __init__(self, arestas: list):
self.adj = [[] for _ in range(len(arestas))]
self.adiciona_arestas(arestas)
def __init__(self, edges: list):
self.adj = [[] for _ in range(len(edges))]
self.add_edges(edges)

def adiciona_arestas(self, arestas: list) -> None:
"""Adiciona todas as arestas ao grafo
def add_edges(self, edges: list) -> None:
"""Add all edges to the graph.

Args:
arestas (list): a lista contendo todas as definições de arestas do grafo
edges (list): The list containing all edge definitions of the graph.
"""
for i in range(len(arestas)):
for j in range(len(arestas[i])):
self.__adiciona_aresta(i, arestas[i][j])
for i in range(len(edges)):
for j in range(len(edges[i])):
self.__add_edge(i, edges[i][j])

def __adiciona_aresta(self, u: int, v: int) -> None:
"""Adiciona a aresta na matriz de adjacência
def __add_edge(self, u: int, v: int) -> None:
"""Add the edge to the adjacency matrix.

Args:
u (int): vértice u
v (int): vértice v
u (int): Vertex u
v (int): Vertex v
"""
if v not in self.adj[u]:
self.adj[u].append(v)
Expand All @@ -41,65 +41,65 @@ def __adiciona_aresta(self, u: int, v: int) -> None:
self.adj[v].append(u)

def bfs(self, start: int) -> list:
"""Executa a busca em largura a partir do vértice start
"""Performs breadth-first search starting from the start vertex.

Args:
start (int): vértice start
start (int): Start vertex

Returns:
list: lista com a ordem de vértices visitados
list: List with the order of visited vertices.
"""
fila = deque()
fila.append(start)
visitados = []
visitados.append(start)
queue = deque()
queue.append(start)
visited = []
visited.append(start)

while fila:
u = fila.popleft()
while queue:
u = queue.popleft()

for v in self.adj[u]:
if v not in visitados:
fila.append(v)
visitados.append(v)
if v not in visited:
queue.append(v)
visited.append(v)

return visitados
return visited

def dfs(self, start: int) -> list:
"""Executa a busca em profundidade a partir do vértice start
"""Performs depth-first search starting from the start vertex.

Args:
start (int): vértice start
start (int): Start vertex

Returns:
list: lista com a ordem de vértices visitados
list: List with the order of visited vertices.
"""
visitados = []
visitados.append(start)
pilha = [start]
visited = []
visited.append(start)
stack = [start]

while pilha:
u = pilha.pop()
while stack:
u = stack.pop()

if u not in visitados:
visitados.append(u)
if u not in visited:
visited.append(u)

for v in self.adj[u][::-1]:
if v not in visitados:
pilha.append(v)
if v not in visited:
stack.append(v)

return visitados
return visited


arestas = [
[1, 2, 5], # Vizinhos do vértice 0.
[0, 2], # Vizinhos do vértice 1.
[0, 1, 3, 4], # Vizinhos do vértice 2.
[2, 4, 5], # Vizinhos do vértice 3.
[2, 3], # Vizinhos do vértice 4.
[0, 3], # Vizinhos do vértice 5.
edges = [
[1, 2, 5], # Neighbors of vertex 0.
[0, 2], # Neighbors of vertex 1.
[0, 1, 3, 4], # Neighbors of vertex 2.
[2, 4, 5], # Neighbors of vertex 3.
[2, 3], # Neighbors of vertex 4.
[0, 3], # Neighbors of vertex 5.
]

grafo = Grafo(arestas)
print(grafo.adj)
print(grafo.bfs(0))
print(grafo.dfs(0))
graph = Graph(edges)
print(graph.adj)
print(graph.bfs(0))
print(graph.dfs(0))
Loading