Git Product home page Git Product logo

var-solutions / algorithms Goto Github PK

View Code? Open in Web Editor NEW
749.0 26.0 1.0K 4.38 MB

A repository of different Algorithms and Data Structures implemented in many programming languages.

Home Page: https://var-solutions.github.io/Algorithms/

License: MIT License

C++ 54.71% Ada 0.49% Java 9.45% Python 9.94% Go 2.15% C 9.32% Ruby 1.29% Lua 0.10% Swift 0.66% JavaScript 2.24% Makefile 0.17% R 0.06% C# 2.68% Brainfuck 0.05% Haskell 0.06% Jupyter Notebook 5.45% D 0.04% Common Lisp 0.41% Rust 0.73% TypeScript 0.01%
algorithms data-structures cpp python javascript java ruby swift golang c

algorithms's Introduction

VAR-solutions.github.io

algorithms's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms's Issues

Centroid of a given Tree | C++

Algorithm to find centroid of a given tree.
Centroid of a Tree is a node which if removed from the tree would split it into a ‘forest’, such that any tree in the forest would have at most half the number of vertices in the original tree.
Language - C++

add efficient algorithms

#1 add cpp, python, java or any language program, also try to save that program in respective algorithm folder

add description file for every algorithm in Markdown language

  • Add description file for every algorithm in the repository.
  • The file must be written in Markdown language.
  • Name of file should be: algorithm_name.md.
  • Description file should be inside the algorithm's folder as: ALGORITHM_NAME/algorithm_name.md.
  • The file should contain the basic information about the algorithm, i.e., where, why and how it is used.
  • It should also contain the pseudocode of the algorithm.

PR is not getting merged

Why is PR is not getting merged to the main branch?. The last change that is merged is around 3 months old. is the repo not active?

Create 8_Puzzle_Problem.py

mport copy
from prettytable import PrettyTable

inputMatrix = [[1, 2, 3], [4, 6, 0], [7, 5, 8]]
outputMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]

class Astar:

def init(self): # Object containing matrix, flag(loc of swap), and elements
self.grid = [] # of the exp f = g + h initialized to zero
self.flag = ""
self.gCost = 0
self.hCost = 0
self.fCost = 0
def generateNeighbours(inMatrix): # Generating neighbours
slot = () # 1. Finding the void place of the current matrix
neighbours = [] # 2. Checking and swapping values to generate neighbours
for i in range(0, len(inMatrix.grid)):
for j in range(0, len(inMatrix.grid)):
if inMatrix.grid[i][j] == 0:
slot = (i, j)
break

x, y = slot

if inMatrix.flag == "":
if x + 1 <= len(inMatrix.grid) - 1:
tobj = Astar()
tobj.grid = copy.deepcopy(inMatrix.grid)
tobj.grid[x][y], tobj.grid[x + 1][y] = tobj.grid[x + 1][y], tobj.grid[x][y]
tobj.flag = "up"
tobj.gCost = inMatrix.gCost + 1
tobj.hCost = findHeuristicValue(tobj.grid)
tobj.fCost = tobj.gCost + tobj.hCost
neighbours.append(tobj)

if x - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x - 1][y] = tobj.grid[x - 1][y], tobj.grid[x][y]
	tobj.flag = "down"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y - 1] = tobj.grid[x][y - 1], tobj.grid[x][y]
	tobj.flag = "left"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y + 1 <= len(inMatrix.grid) - 1:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y + 1] = tobj.grid[x][y + 1], tobj.grid[x][y]
	tobj.flag = "right"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if inMatrix.flag == "up":
if x + 1 <= len(inMatrix.grid) - 1:
tobj = Astar()
tobj.grid = copy.deepcopy(inMatrix.grid)
tobj.grid[x][y], tobj.grid[x + 1][y] = tobj.grid[x + 1][y], tobj.grid[x][y]
tobj.flag = "up"
tobj.gCost = inMatrix.gCost + 1
tobj.hCost = findHeuristicValue(tobj.grid)
tobj.fCost = tobj.gCost + tobj.hCost
neighbours.append(tobj)

if y - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y - 1] = tobj.grid[x][y - 1], tobj.grid[x][y]
	tobj.flag = "left"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y + 1 <= len(inMatrix.grid) - 1:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y + 1] = tobj.grid[x][y + 1], tobj.grid[x][y]
	tobj.flag = "right"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if inMatrix.flag == "down":
if x - 1 >= 0:
tobj = Astar()
tobj.grid = copy.deepcopy(inMatrix.grid)
tobj.grid[x][y], tobj.grid[x - 1][y] = tobj.grid[x - 1][y], tobj.grid[x][y]
tobj.flag = "down"
tobj.gCost = inMatrix.gCost + 1
tobj.hCost = findHeuristicValue(tobj.grid)
tobj.fCost = tobj.gCost + tobj.hCost
neighbours.append(tobj)

if y - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y - 1] = tobj.grid[x][y - 1], tobj.grid[x][y]
	tobj.flag = "left"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y + 1 <= len(inMatrix.grid) - 1:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y + 1] = tobj.grid[x][y + 1], tobj.grid[x][y]
	tobj.flag = "right"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if inMatrix.flag == "left":
if x + 1 <= len(inMatrix.grid) - 1:
tobj = Astar()
tobj.grid = copy.deepcopy(inMatrix.grid)
tobj.grid[x][y], tobj.grid[x + 1][y] = tobj.grid[x + 1][y], tobj.grid[x][y]
tobj.flag = "up"
tobj.gCost = inMatrix.gCost + 1
tobj.hCost = findHeuristicValue(tobj.grid)
tobj.fCost = tobj.gCost + tobj.hCost
neighbours.append(tobj)

if x - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x - 1][y] = tobj.grid[x - 1][y], tobj.grid[x][y]
	tobj.flag = "down"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y - 1] = tobj.grid[x][y - 1], tobj.grid[x][y]
	tobj.flag = "left"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if inMatrix.flag == "right":
if x + 1 <= len(inMatrix.grid) - 1:
tobj = Astar()
tobj.grid = copy.deepcopy(inMatrix.grid)
tobj.grid[x][y], tobj.grid[x + 1][y] = tobj.grid[x + 1][y], tobj.grid[x][y]
tobj.flag = "up"
tobj.gCost = inMatrix.gCost + 1
tobj.hCost = findHeuristicValue(tobj.grid)
tobj.fCost = tobj.gCost + tobj.hCost
neighbours.append(tobj)

if x - 1 >= 0:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x - 1][y] = tobj.grid[x - 1][y], tobj.grid[x][y]
	tobj.flag = "down"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

if y + 1 <= len(inMatrix.grid) - 1:
	tobj = Astar()
	tobj.grid = copy.deepcopy(inMatrix.grid)
	tobj.grid[x][y], tobj.grid[x][y + 1] = tobj.grid[x][y + 1], tobj.grid[x][y]
	tobj.flag = "right"
	tobj.gCost = inMatrix.gCost + 1
	tobj.hCost = findHeuristicValue(tobj.grid)
	tobj.fCost = tobj.gCost + tobj.hCost
	neighbours.append(tobj)

return neighbours
def findHeuristicValue(inMatrix): # Comparing position of values of current matrix to the ones of desired matrix
h = 0
for i in range(0, len(inMatrix)):
for j in range(0, len(inMatrix)):
if inMatrix[i][j] != outputMatrix[i][j]:
h += 1
return h

def findBestMatrix(openList): # Select best matrix from open list and return it
bestmatrix = Astar()
mincost = float("inf")
for matrix in openList:
if matrix.fCost <= mincost:
mincost = matrix.fCost
bestmatrix = matrix
return bestmatrix

def printMatrix(matrix): # Formatting matrices
p = PrettyTable()
for row in matrix:
p.add_row(row)
return p.get_string(header=False, border=False)

def pathFinder():
openList = []
closedList = []
current = Astar()
current.grid = inputMatrix
openList.append(current)
steps = 0

print("Input Matrix -->") # Printing Input Matrix
print(printMatrix(current.grid))
print("Output Matrix -->") # Printing Output Matrix
print(printMatrix(outputMatrix))
print()
print()

while True:
current = findBestMatrix(openList)
openList.remove(current)
closedList.append(current)

print("Step {0}:".format(str(steps)))  # Printing steps
print(printMatrix(current.grid))       # Printing matrices
print()

if findHeuristicValue(current.grid) == 0:
	exit(0)

neighbourList = generateNeighbours(current)     # Generating neighbours from the selected matrix
for neighbour in neighbourList:
	if neighbour not in openList:
		openList.append(neighbour)              # Appending neighbours in the open list
steps += 1                                      # Incrementing steps

pathFinder()
8Puzzle

add README to Searching Directory

Add a README.md file inside the Searching Directory. The file should contain a table that has

  • Columns of searching algorithms and programming languages(C, CPP, Python, Java, etc.)
  • Add a row of each searching algorithm and mark a ✔️ with a LINK to code if it is currently implemented in the corresponding programming language, else leave the cell blank.
  • Do it for each searching algorithm.
  • You can also change file names as per the algorithm to simplify the task. But do not forget to follow the nomenclature as:
    algorithm-name/ProgrammingLanguage/algorithm_code.extension

Refer to Sorting README and implement in the same manner.Refer to Markdown Cheatsheet or Makrdown Guide to get help with writing Markdown.

Example Scrrenshot is given below:
ex-sortingREADME

Linear Search & Bubble Sort

Hello,
I have contributed code for Linear Search in C++ and Bubble Sort in C Language.
Hope you'll accept it.

Issue with fork ?

I am trying to clone this repo after forking but its giving me errors.

Convert Number to words

I would like to add a algorithm which converts number entered by user into words.
example:
Input 100
Output One Hundred
This will be useful when implementing a feature like automatically making a dummy cheque for user with amount filled.

add algorithms and data structures in any language

Dated: Oct-07-2020
Regarding Delay in reviewing PR
Hello, everyone, I am one of the maintainer of this repository. I know most of the PRs are the part of hacktoberfest and most of you are worried will your PRs will be eligible for Hacktoberfest 2020 or not. Since, all the maintainers are busy, it will take some time to review all the PRs. We have to review each PR and it may take some time.
Also, the rules of Hacktoberfest have been changed to reduce spam PRs. There are many SPAM PRs with invalid code or no code. So, we have to review each PR one by one. Due to the changes in rules, the burden has been shifted to maintainers.

About adding and then removing hacktoberfest-accepted label, it was just to check and verify the eligibility of the label. Do not consider it as making your PR invalid.

All the PRs will be checked and reviewed

Thanks and Happy coding 💻 👨‍💻 👩‍💻 💻

implement graph algorithms

  • minimum spanning tree

  • single source shortest path

  • all pair shortest path

  • maximum flow

  • any other type of graph algorithms

Wrong folder

The files euclidean_algo_GCD_basic.py and euclidean_algo_GCD_extended.py are placed in wrong folder. They should be place in Algorithms/Mathematics/GCD/ and removed from Algorithms/Mathematics/euclidean distance/.

Subset Sum Problem in C++

I would like to contribute to the repository by writing the program for the famous subset sum problem for hacktoberfest. Kindly assign the issue to me :)

LCSP

write a program to find the strings that make lcsp length in any language

Longest Increasing Subsequence in O(N log N)

I would like to implement the Longest Increasing Subsequence problem using Binary Search and Dynamic Programming in worst-case complexity of N log N as a part of Hacktober Fest 2020.

Binary search for 2d array in C++

It searches for a value in a 2d m*n matrix.
Properties of matrix:

  1. Integers in each row are sorted from left to right.
  2. The first integer of each row is greater than the last integer of the previous row.

Maximize expression | Dynamic Programming | Python

Add an algorithm to maximize an expression by computing and maximizing subexpressions and placing parentheses.
Files that would change after the PR: Dynamic Programming/Maximize Expression/maximize_expression.py

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.