From ac1adadc680b990b59b581aceb3c8c767f4ed838 Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Wed, 29 May 2024 22:34:21 -0300 Subject: [PATCH] Adding --- estrutura_de_dados/grafos/Graph.class | Bin 0 -> 1127 bytes estrutura_de_dados/grafos/Main.class | Bin 0 -> 697 bytes estrutura_de_dados/grafos/Main.java | 68 ++++++++++++++ estrutura_de_dados/grafos/Vertex.class | Bin 0 -> 734 bytes estrutura_de_dados/grafos/a_estrela.py | 120 +++++++++++++++++++++++++ 5 files changed, 188 insertions(+) create mode 100644 estrutura_de_dados/grafos/Graph.class create mode 100644 estrutura_de_dados/grafos/Main.class create mode 100644 estrutura_de_dados/grafos/Main.java create mode 100644 estrutura_de_dados/grafos/Vertex.class create mode 100644 estrutura_de_dados/grafos/a_estrela.py diff --git a/estrutura_de_dados/grafos/Graph.class b/estrutura_de_dados/grafos/Graph.class new file mode 100644 index 0000000000000000000000000000000000000000..964b9e8ffcbcfab19ae5188b972ed85c67e30bec GIT binary patch literal 1127 zcmaJ=+fEZv6kVs&cI+@*+RlJfGjAAfNiaubP$V)TrjCG(E(wP$RMZd#u z@Wn(R)x`K_;xCyP*O^`@V9X@@oVCx|YhTWpUw^)S2e5`!4FM=Bf;vJ73-s-phi0;D zR!Yg|gB10(RgCDkhEai@ z^N|8U*Li3A^<^}SV?xEGj_Z;d&e>JZe#=TqwwHM#g{F1fzzo^+2n@wDiAE(IJ*QN} z1P0F1JgQdBqehcaC{kr!M;r;Nn3g3l9M4=9N#ry{F)t9=c1jh~t5sR!=7qM?9k`t( zoo$qzUb?Kho{}GXj_)LLTsHPC%J%Xe>}Rz%1-tsfER^Zd*=7<=#tR3N}NAY zw@cKzlOwhoZ)QxjR;k^+?U}{BE%Tr$xz=L+yRQEhE%lVbE0=e>yDTMJ2>dR9^j-eUj$LRY^ zfz>gr*>@EKbfMv)#k^TRPyyUKWJTy zm?mQdx5<=+)|p%3`awo4oM5busQ`Xrq=mmB%Gzh^m{ZX5`J5k!O8(*r4Ep;%;-Ney zJSQRXMTs_t5^_%&L(U}N&twc??q?Ch9MXKp8!%AfxZiWQcX+}dIrbZmN18Wqm+jnZ Xw=+tD9H}^)abr#^9rXJo1n}T5L!ad> literal 0 HcmV?d00001 diff --git a/estrutura_de_dados/grafos/Main.class b/estrutura_de_dados/grafos/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..511b990d7e48a35fd18b7bcfdc48cfba2c82db95 GIT binary patch literal 697 zcmZuvU2oGc6g_U!rVSZeH(EB>pyM;yAY~wU$rwUNph_J>DvC6aKyGV_tJIlNXJY&- z_OK!*@dNl#i0c+uk;sF6bA5b1uKeTYx9% zHA8V*DyerE@}3`+Q9{{<W6{MDE-)m%(tWxFSB7sq_24=rj9k7oY%a!Gn#(~!{D@&EbWTt z(tyi`EHLq~j!k+5=F5R*ha#2Hm8Mtfmn4EBWzhJaRc3_L^`4cw#JK;Kc{ + + private int ID; + private T element; + + public Vertex(T element, int ID) + { + this.setElement(element); + this.setID(ID); + } + + public void setElement(T element) { this.element = element; } + public T getElement() { return this.element; } + public void setID(int element) { this.ID = ID; } + public int getID() { return this.ID; } +} + +class Graph{ + + private List>> adjacencies_list; + private boolean is_digraph; + + public Graph(boolean is_digraph) + { + this.adjacencies_list = new LinkedList<>(); + this.is_digraph = is_digraph; + } + + public int get_vertex_n() { return this.adjacencies_list.size(); } + public int get_edges_n() { return this.adjacencies_list.size()-1; } + + public void set_adjacency(Vertex v1, Vertex v2) + { + + if(this.adjacencies_list.get(v1.getID()) == null) this.adjacencies_list.add(v1.getID(), new ArrayList<>()); + this.adjacencies_list.get(v1.getID()).add(v1.getID(), v2); + + if(!this.is_digraph) + { + if(this.adjacencies_list.get(v2.getID()) == null) this.adjacencies_list.add(v2.getID(), new ArrayList<>()); + this.adjacencies_list.get(v2.getID()).add(v2.getID(), v1); + } + } + + public void bfs(Vertex origin, Vertex destination) + { + Queue bfs_queque; + } +} + +public class Main +{ + public static void main(String[] args) + { + final boolean is_digraph = true; + + System.out.println("Graph is initializted..."); + + Graph graph = new Graph(is_digraph); + + System.out.println("Mounting a graph..."); + + graph.set_adjacency(new Vertex<>(0, 0), new Vertex<>(0, 0)); + + } +} \ No newline at end of file diff --git a/estrutura_de_dados/grafos/Vertex.class b/estrutura_de_dados/grafos/Vertex.class new file mode 100644 index 0000000000000000000000000000000000000000..fe29074d69170222ed106ca30507349f19223cc5 GIT binary patch literal 734 zcmZuu+fKqj5ItLDJLjk93Ok+ksQ*==hFw1@;@WO6KvcpQ{Kw#=0dCE}j_K$pDXi7#8)(ug4tK*ToMKNfMXYLvAH;^7!^-|J`gk}*k@9V~ zQ8m@YZ_SL6qk6mSjeF}zUC4Qw)}Ve1Fb CJatq6 literal 0 HcmV?d00001 diff --git a/estrutura_de_dados/grafos/a_estrela.py b/estrutura_de_dados/grafos/a_estrela.py new file mode 100644 index 0000000..7c9723f --- /dev/null +++ b/estrutura_de_dados/grafos/a_estrela.py @@ -0,0 +1,120 @@ +class Node(): + """A node class for A* Pathfinding""" + + def __init__(self, parent=None, position=None): + self.parent = parent + self.position = position + + self.g = 0 + self.h = 0 + self.f = 0 + + def __eq__(self, other): + return self.position == other.position + + +def astar(maze, start, end): + """Returns a list of tuples as a path from the given start to the given end in the given maze""" + + # Create start and end node + start_node = Node(None, start) + start_node.g = start_node.h = start_node.f = 0 + end_node = Node(None, end) + end_node.g = end_node.h = end_node.f = 0 + + # Initialize both open and closed list + open_list = [] + closed_list = [] + + # Add the start node + open_list.append(start_node) + + # Loop until you find the end + while len(open_list) > 0: + + # Get the current node + current_node = open_list[0] + current_index = 0 + for index, item in enumerate(open_list): + if item.f < current_node.f: + current_node = item + current_index = index + + # Pop current off open list, add to closed list + open_list.pop(current_index) + closed_list.append(current_node) + + # Found the goal + if current_node == end_node: + path = [] + current = current_node + while current is not None: + path.append(current.position) + current = current.parent + return path[::-1] # Return reversed path + + # Generate children + children = [] + for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares + + # Get node position + node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1]) + + # Make sure within range + if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0: + continue + + # Make sure walkable terrain + if maze[node_position[0]][node_position[1]] != 0: + continue + + # Create new node + new_node = Node(current_node, node_position) + + # Append + children.append(new_node) + + # Loop through children + for child in children: + + # Child is on the closed list + for closed_child in closed_list: + if child == closed_child: + continue + + # Create the f, g, and h values + child.g = current_node.g + 1 + child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2) + child.f = child.g + child.h + + # Child is already in the open list + for open_node in open_list: + if child == open_node and child.g > open_node.g: + continue + + # Add the child to the open list + open_list.append(child) + + +def main(): + + maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] + + start = (0, 0) + end = (7, 6) + + path = astar(maze, start, end) + print(path) + + +if __name__ == '__main__': + main() \ No newline at end of file