0% found this document useful (0 votes)
7 views

Codesjava

java codes

Uploaded by

jerryt2714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Codesjava

java codes

Uploaded by

jerryt2714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dijkstra algorithm

import java.util.*;

public class Main{


int mn=Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter nodes: ");
int V=sc.nextInt();
int graph[][]=new int[V][V];
for(int i=0;i<V;i++){
for(int j=0;j<V;j++)
graph[i][j]=0;
}

System.out.print("Enter edges: ");


int e=sc.nextInt();
for(int i=0;i<e;i++){
System.out.print("Source nodes: ");
int u=sc.nextInt();
System.out.print("Enter dest.: ");
int vv=sc.nextInt();
System.out.print("There distance: ");
int dist=sc.nextInt();
graph[u][vv]=dist;
graph[vv][u]=dist;
}

System.out.print("Enter the scource: ");


int src=sc.nextInt();
Main m=new Main();
m.dij(graph,src,V);
sc.close();
}

int mindist(int dist[],Boolean spset[],int v){


int min=mn, minindex=-1;
for(int i=0;i<v;i++){
if(!spset[i] && dist[i]<= min){
min=dist[i]; minindex=i;
}
}
return minindex;
}

void dij(int graph[][],int src,int V){


int d[]=new int[V];
Boolean spset[]=new Boolean[V];
for(int i=0;i<V;i++){
d[i]=mn;
spset[i]=false;
}
d[src]=0;
for(int c=0;c<V-1;c++){
int u=mindist(d, spset, V);
spset[u]=true;
for(int v=0;v<V;v++){
if (!spset[v] && graph[u][v]!=0 && d[u]!=mn && d[u]+graph[u][v]<d[v])
d[v]=d[u]+graph[u][v];
}
}
System.out.println("nodes \t Distance");
for(int i=0;i<V;i++)
System.out.print("For: "+i+"\t"+" distance from source is: "+d[i]+"\n");
}
}
// output
Enter nodes: 5
Enter edges: 6
Source nodes: 0
Enter dest.: 1
There distance: 10
Source nodes: 0
Enter dest.: 2
There distance: 3
Source nodes: 1
Enter dest.: 2
There distance: 1
Source nodes: 1
Enter dest.: 3
There distance: 2
Source nodes: 2
Enter dest.: 3
There distance: 8
Source nodes: 3
Enter dest.:
4
There distance: 7
Enter the scource: 0
nodes Distance
For: 0 distance from source is: 0
For: 1 distance from source is: 4
For: 2 distance from source is: 3
For: 3 distance from source is: 6
For: 4 distance from source is: 13

Python code
import sys
import heapq

class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)] for row in range(vertices)]

def dijkstra(self, src):


dist = [sys.maxsize] * self.V
dist[src] = 0
spset = [False] * self.V
pq = [(0, src)]

while pq:
(d, u) = heapq.heappop(pq)
if spset[u]:
continue
spset[u] = True

for v in range(self.V):
if self.graph[u][v] > 0 and not spset[v] and dist[u] != sys.maxsize and dist[u] + self.graph[u][v]
< dist[v]:
dist[v] = dist[u] + self.graph[u][v]
heapq.heappush(pq, (dist[v], v))

print("Node \t Distance from Source")


for i in range(self.V):
print(f"{i} \t {dist[i]}")

if __name__ == "__main__":
V = int(input("Enter nodes: "))
g = Graph(V)

e = int(input("Enter edges: "))


for _ in range(e):
u = int(input("Source node: "))
v = int(input("Enter dest.: "))
dist = int(input("Their distance: "))
g.graph[u][v] = dist
g.graph[v][u] = dist

src = int(input("Enter the source: "))


g.dijkstra(src)

BigInt
import java.math.BigInteger;
import java.util.Scanner;

public class Squaredc{


public static BigInteger square(BigInteger a) {
String stra=a.toString();
int len=stra.length();
if(len==1){ return a.multiply(a); }
int half=len/2;
String strb=stra.substring(0, half);
String strc=stra.substring(half);
BigInteger b1=new BigInteger(strb);
BigInteger c1=new BigInteger(strc);
BigInteger b2=square(b1);
BigInteger c2=square(c1);
BigInteger bc=b1.multiply(c1);
BigInteger pows=BigInteger.TEN.pow(len - half);
BigInteger result =
b2.multiply(pows.multiply(pows)).add(bc.multiply(BigInteger.TWO).multiply(pows)).add(c2);
return result;
}

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.print("Enter number: ");
BigInteger a=sc.nextBigInteger();
BigInteger rs=square(a);
System.out.println("Square is: " + rs);
sc.close();
}
}

Python
from decimal import Decimal

def square(a):
stra = str(a)
length = len(stra)
if length == 1:
return a * a
half = length // 2
strb = stra[:half]
strc = stra[half:]
b1 = Decimal(strb)
c1 = Decimal(strc)
b2 = square(b1)
c2 = square(c1)
bc = b1 * c1
pows = Decimal(10) ** (length - half)
result = b2 * (pows * pows) + bc * Decimal(2) * pows + c2
return result

if __name__ == "__main__":
a = Decimal(input("Enter number: "))
rs = square(a)
print("Square is:", rs)

You might also like