Posts

Showing posts from April, 2023

BOOKSTORE DATABASE

 CREATE DATABASE BOOKSTORE; USE BOOKSTORE; CREATE TABLE BOOKTABLE (ID INT NOT NULL,  BOOK_NAME VARCHAR(100) NOT NULL, BOOK_PRICE INT NOT NULL, PRIMARY KEY (ID) ); ALTER TABLE BOOKTABLE ADD (YEAROFPUBLICATION INT); INSERT INTO BOOKTABLE (ID,BOOK_NAME,BOOK_PRICE, YEAROFPUBLICATION) VALUES (100,'Finding Me, by Viola Davis',265,2022); INSERT INTO BOOKTABLE (ID,BOOK_NAME,BOOK_PRICE, YEAROFPUBLICATION) VALUES (101,'The Psychology of Money by Morgan Housel',210,2020); INSERT INTO BOOKTABLE (ID,BOOK_NAME,BOOK_PRICE, YEAROFPUBLICATION) VALUES (102,'Naked Statistics – Stripping the Dread from the Data by Charles Wheelan', 3899,2013); DROP TABLE BOOKTABLE; TRUNCATE TABLE BOOKTABLE; RENAME TABLE BOOKTABLE TO MYSTORE; UPDATE BOOKTABLE SET BOOK_PRICE=2000 WHERE ID=102; DELETE FROM BOOKTABLE WHERE ID=102; SELECT * FROM BOOKTABLE; SELECT * FROM MYSTORE;

https://dev.mysql.com/get/Downloads/MySQLInstaller/mysql-installer-community-8.0.33.0.msi

Traveling Salesman problem using Branch-and-Bound technique.

 Algorithm: Algorithm TSPBranchAndBound(distances) {  n := size(distances)  best_cost := infinity  best_path := null  queue := [(0, [0], set([0]))] // (cost, path, visited)  while queue is not empty do  cost, path, visited := pop(queue)  if length(path) = n then  cost += distances[path[-1], 0]  if cost < best_cost then  best_cost := cost  best_path := path  else  for i in range(n) do  if i not in visited then  new_path := path + [i] new_visited := visited.union(set([i]))  new_cost := cost + distances[path[-1], i]  for j in range(n) do  if j not in new_visited then  new_cost += np.min(distances[j, list(new_visited)])  if new_cost < best_cost then  push(queue, (new_cost, new_path, new_visited))  return best_path, best_cost } Source Code: import numpy as np def tsp_branch_and_bound(distances):  n = distances.shape[0]  # Initialize variables  best_cost = np...

Hamiltonian cycle algorithm using Backtracking technique.

Algorithm: Algorithm NextValue(K) { repeat { x[k] := (x[k+1] mod (n+1)); if (x[k] = 0) then return; if (G[x[k-1], x[k]] != 0) then { for j := 1 to k-1 do if (x[j] = x[k]) then break; if (j = k) then if ((k < n) or ((K = n) and G[x[n], x[l]] != 0)) then return; } }until false } Algorithm Hamiltonian(k) { repeat { NextValue(k); if (x[k]=0) then return; if (k=n) then write (x[1:n]); else Hamiltonian(k + 1); } until (false); } Source Code: def hamiltonian_cycle(graph):  n = len(graph)  path = [-1] * n  def is_valid(v, k):  if graph[path[k-1]][v] == 0:  return False  if v in path[:k]:  return False  return True  def solve(k):  if k == n:  if graph[path[k-1]][path[0]] == 1:  return True  else: return False  for v in range(1, n):  if is_valid(v, k):  path[k] = v  if solve(k+1):  return True  path[k] = -1  return False  path[0] = 0  if solve(1):  return path  else: ...

8 Queens algorithm using Backtracking technique.

 Algorithm NQueens(k, n) {  for i := 1 to n do  {  if Place(k, i) then  {  x[k]:=i;  if (k = n) then write (x[l : n]);  else NQueens(k + 1 , n) ;  }  } } Algorithm Place(k, i) {  for j := 1 to k — 1 do  if ((x[j] =i)  or (Abs (x[j] — i) = Abs(j — k)))  then return false;  return true; } Source Code: def nqueens(k, n, x):  for i in range(1, n+1):  if place(k, i, x):  x[k] = i  if k == n:  print_board(x)  else:  nqueens(k+1, n, x) def place(k, i, x):  for j in range(1, k):  if x[j] == i or abs(x[j] - i) == abs(j - k):  return False  return True def print_board(x):  n = len(x)  for i in range(n):  row = ['Q' if x[i] == j+1 else '.' for j in range(n)]  print(' '.join(row)) n = 8 x = [0] * (n+1) nqueens(1, n, x) output 1 5 8 6 3 7 2 4

Traveling Salesman algorithm using Dynamic Programming technique.

 Algorithm: Algorithm TSP(distances) {  n:=length(distances);  memo:=a 2D array of size n x (2^n) filled with None;  dp(pos, visited)  {  if visited == (2^n) - 1 then  return distances[pos][0], [0];  if memo[pos][visited] is not None then  return memo[pos][visited];  shortest_dist := Infinity;  shortest_path := [];  for next_pos from 0 to n-1 do  {  if visited & (1 << next_pos) == 0 then  {  dist, path = dp(next_pos, visited | (1 << next_pos));  dist := dist + distances[pos][next_pos];  if dist < shortest_dist then  {  shortest_dist := dist; shortest_path := [pos] + path;  }  }  }  memo[pos][visited] := (shortest_dist, shortest_path);  return memo[pos][visited];  }  path := dp(0, 1);  return path; } Source Code: import sys from typing import List, Tuple def tsp(distances: List[List[int]]) -> Tuple[int, List[int]]:  n = le...

Floyd’s algorithm using Dynamic Programming technique.

  Algorithm:            Algorithm AllPaths(cost, A, n) { for i :=1 to n do for j := 1 to n do A[i,j]:= cost[i,j]; for k := 1 to n do for i := 1 to n do for j := 1 to n do A[i, j] := min(A[i,j], A[i, k] + A[k,j]); }   Source Code: def floyd(graph): n = len(graph) dist = graph for k in range(n): for i in range(n): for j in range(n): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist graph = [[0, 4, 11], [6, 0, 2], [3, float('inf'), 0]] print(floyd(graph))  

Multi-Stage Graphs using Dynamic Programming technique.

  Algorithm: Algorithm BGraph(G, k, n, p){ bcost[l]:=0.0; for j:=2 to n do      { Let r be such that (r, j) is an edge of G and bcost[r] + c[r,j] is minimum; bcost[j]:=bcost[r]+c[r,j]; d[j]:=r;      } p[l]:1; p[k]:=n; for j:=k— 1 to 2 do p[j]:=d[p[j+1]];} Source Code: def shortestDist(graph): global INF dist = [0] * N path = [None] * N dist[N - 1] = 0 for i in range(N - 2, -1, -1): dist[i] = INF for j in range(N): if graph[i][j] == INF: continue if dist[i] > graph[i][j] + dist[j]: dist[i] = graph[i][j] + dist[j] path[i] = j shortest_path = [0] node = 0 while node != N-1: node = path[node] shortest_path.append(node) return dist[0], shortest_path N = 8 INF = 999999999999 graph = [[INF, 1, 2, 5, INF, INF, INF, INF], [INF, INF, INF, INF, 4, 11, INF, INF], [INF, INF, INF, INF, 9, 5, 16, INF], [INF, INF, INF, ...

Kruskal algorithm using greedy technique.

Algorithm: Algorithm Kruskal(E, cost, n, t) { Construct a heap out of the edge costs using Heapify; for i:=1 to n, do parent[i]:=—1; i := O; mincost := 0.0; while ( (i < n — 1) and (heap not empty)) do       { Delete a minimum cost edge (u, v) from the heap and reheapify using Adjust; j:=Find(u); k := Find(v); if (j!=k) then        { i:=i+1; t[i, 1] := u; t[i, 2]:=v; mincost := mincost + cost[u, v]; Union(j, k); }                 } if (i != n — 1) then write ("No spanning tree"); else return mincost; } Source Code: def kruskal(E, cost, n): # E is the edge set, cost is the cost matrix, n is the number of vertices T = [] # T is the set of edges in the tree S = [] # S is the set of vertices in the tree while len(T) < n ...

Prim’s algorithm using greedy technique.

  Algorithm: Algorithm prim(E,cost,n,t){ Let (k,l) be an edge of minimum cost in E; mincost :=cost[k,l]; t[l,l]:=k;t[1,2]:=t; for i:=1 to n do // lnitialize near. if (cost[i,l] < cost[i,k]) then near[i]:=l; else near[l]:=k; near[k]:=near[l]:=0; for i:=2 to n-1 do{ Let j be an index such that near[j]!=o and cost[j,near[j]] is minimum; t[i,l]:=j;t[i,2]:=near[j]; mincost:=mincost+cost[j,near[j]]; near[j]:=0; for k:=1 to n do // Update near[]. if (near[k]!=0) and (cost[k,near[k]]>cost[k,j])) then nwar[k] :=j;          } return mincost;} Source Code: def prim(E, cost, n, t): S = [t] T = [] while len(S) < n: min_cost = float('inf') for i in S: for j in range(n): if j not in S and cost[i][j] < min_cost: min_cost = cost[i][j] min_edge = (i, j) S.append(min_edge[1]) T.append(min_edge) return T E = [(0, 1), (0, 2), (0, 3), (1, ...

Single-Source Shortest Path algorithm using Greedy technique

 Algorithm ShortestPaths(v,cost,dist,n) { for i :=1to n do { S[i]:=false; dist[i]:=cost[v,i]; } S[v]:=true; dist[v] :=0.0; { Choose u from among those vertices not in S such that dist[u]is minimum; S[u]:=true;// Put u in S. for (each w adjacent to u with S[w]= false)do { if { dist[w] >dist[u]+cost[u, w])) then dist[w] :=dist[u]+cost[u, w]; } } } } Source Code: def dj(graph,src): dist=[99 for i in range(len(graph))] visited=[False for i in range(len(graph))] for i in range(len(graph)): if(graph[src][i]<=dist[i]): dist[i]=graph[src][i] dist[src]=0 visited[src]=True count=0   while(count<len(graph)): min=99 for i in range(len(graph)): if(visited[i]==False and min>dist[i]): min=dist[i] v=i visited[v]=True count=count+1 for i in range(len(graph)): if(dist[i]>dist[v]+graph[v][i]): dist[i]=dist[v]+graph[v][i]   for i in range(len(dist)): print...

Knapsack problem using Greedy technique

  Algorithm:            Algorithm GreedyKnapsack(m,n) { for i:=1 to n do x[i] :=0.0; U := m; for i:= 1 to n do { if w[i]> U)then break;   X[i] := 1.0; U:=U-w[i]; } if (i<=n) then x[i] := U/w[i]; } Source Code: def knapsack_greedy(values, weights, capacity): n = len(values) ratios = [(values[i] / weights[i], i) for i in range(n)]   ratios.sort(reverse=True) selected_item=[]   total_value = 0 for ratio, i in ratios: if weights[i] <= capacity: selected_items.append(i)   total_value += values[i] capacity -= weights[i] return selected_items, total_value values = [10, 20, 15, 25, 30] weights = [5, 7, 3, 9, 2] capacity = 15 selected_items, total_value = knapsack_greedy(values, weights, capacity) print("Selected items:", selected_items) print("Total value:", total_value)

Strassen Algorithm using Divide and Conquer

Algorithm Strassen(A,B,C,n) { If(n==2) then { C11 := A11 B11 + A12 B12;   C12 := A11 B12 + A12 B22; C21 := A21 B11+ A22 B21;   C22 := A21 B21+ A22 B22; } else { Partition A into 4 matrices A11, A12, A21, A22 each of size n/2; Partition B into 4 matrices B11,B12,B21,B22 each of size n/2; P1 := Strassen(A11,(B12-B22),C,n/2);   P2 := Strassen((A11+A12),B22,C,n/2); P3 := Strassen((A21+A22),B11,C,n/2); P4 := Strassen(A22,(B21-B11),CC,n/2); P5 := Strassen((A11+A22),(B11+B22),C,n/2);   P6 := Strassen((A12-A22),(B21+B22),C,n/2);   P7 := Strassen((A11-A21),(B11+B12)C,n/2);   C11 := P5=P4-P2+P6; C12 := P1+P2; C21 := P3+P4; C22 := P1+P5-P3-P7; } Return C; } Source Code: import numpy as np def splitmat(matrix): row, col = matrix.shape row2, col2 = row//2, col//2 return matrix[:row2, :col2], matrix[:row2, col2:], matrix[row2:, :col2], matrix[row2:, col2:] ...