Sorting, Searching, and Graph Algorithms in Computer Science

Classified in Computers

Written at on English with a size of 4.11 KB.

Insertion Sort Algorithm

def insertion_sort(arr) :

for i in range(1, len(arr)):

key = arr[i]

j = i - 1

while j >= 0 and key < arr[j]:

arr[j + 1] = arr[j]

j -= 1

arr[j + 1] = key

# Example usage:

arr = [12, 11, 13, 5, 6]

insertion_sort(arr)

print("Sorted array is:", arr)

Binary Search Algorithm

def binary_search(arr, target):

left, right = 0, len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

left = mid + 1

else:

right = mid - 1

return -1

# Example usage:

arr = [2, 3, 4, 10, 40]

target = 10

result = binary_search(arr, target)

if result != -1:

print("Element is present at index", result)

else:

print("Element is not present in array")

Minimum Cost Spanning Tree (MCST)

A Minimum Cost Spanning Tree (MCST) is a fundamental problem in graph theory and optimization.

It aims to find the subset of edges in a weighted, undirected graph that connects all vertices together with the minimum possible total edge weight.

This subset of edges forms a tree that spans all vertices, hence the name "spanning tree," and the goal is to minimize the total weight, hence "minimum cost."

The most common algorithms to solve this problem are Prim's algorithm and Kruskal's algorithm.

Asymptotic Notation

Asymptotic notation is a mathematical notation used to describe the behavior of a function as its input grows towards infinity. It is commonly used in computer science and mathematics to analyze the efficiency and performance of algorithms. Asymptotic notation provides a concise way to express how the runtime or space complexity of an algorithm scales with the size of the input.

The N-Queens Problem

The N-Queens problem is a classic problem in combinatorial optimization and computer science, often used to demonstrate various problem-solving techniques such as backtracking, recursion, and constraint satisfaction algorithms.

In the N-Queens problem, you're given an N×N chessboard, and the goal is to place N queens on the board in such a way that no two queens threaten each other. A queen threatens another if they share the same row, column, or diagonal.

Solution Approaches:

  • Brute Force: Generate all possible arrangements of queens and check each one for validity. This approach is impractical for larger N due to the combinatorial explosion of possibilities.
  • Backtracking: Use a recursive backtracking algorithm to systematically explore possible placements of queens, backtracking when a placement leads to conflicts, and continue until a valid solution is found or all possibilities are exhausted.
  • Constraint Satisfaction: Formulate the problem as a constraint satisfaction problem (CSP) and apply constraint propagation techniques like forward checking or arc consistency to efficiently find valid queen placements.
  • Optimization Techniques: Apply optimization techniques such as simulated annealing or genetic algorithms.

Entradas relacionadas: