O’zbekiston respublikasi axborot texnoloogiyalari va kommunikatsiyalarni rivojlantirish vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnoloogiyalari universiteti muammo, tahlil va yechim fani bo’yicha laboratoriya ishi-1



Download 36,9 Kb.
bet2/2
Sana29.05.2023
Hajmi36,9 Kb.
#945625
1   2
Bog'liq
Transport masalasi Transport masalasini yechish dasturini yaratish

Amaliy qism: Bu masalani yechishda java dasturlash tilidan foydalandim.
import java.util.*;

public class TransportationProblemSolver {


private static class Node {
String name;
List edges;
int shortestDistance;
boolean visited;
Node previousNode;
public Node(String name) {
this.name = name;
this.edges = new ArrayList<>();
this.shortestDistance = Integer.MAX_VALUE;
this.visited = false;
this.previousNode = null;
}
public void addEdge(Node destination, int weight) {
edges.add(new Edge(destination, weight));
}
}
private static class Edge {
Node destination;
int weight;
public Edge(Node destination, int weight) {
this.destination = destination;
this.weight = weight;
}
}
private static List findShortestPath(Node start, Node end) {
start.shortestDistance = 0;
Queue queue = new PriorityQueue<>(Comparator.comparingInt(node -> node.shortestDistance));
queue.add(start);
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current == end) {
break; // Found the shortest path
}
current.visited = true;
for (Edge edge : current.edges) {
Node next = edge.destination;
int newDistance = current.shortestDistance + edge.weight;
if (!next.visited && newDistance < next.shortestDistance) {
queue.remove(next);
next.shortestDistance = newDistance;
next.previousNode = current;
queue.add(next);
}
}
}
List shortestPath = new ArrayList<>();
Node current = end;
while (current != null) {
shortestPath.add(0, current);
current = current.previousNode;
}
return shortestPath;
}
public static void main(String[] args) {
// Creating nodes
Node nodeA = new Node("A");
Node nodeB = new Node("B");
Node nodeC = new Node("C");
Node nodeD = new Node("D");
Node nodeE = new Node("E");
// Creating edges
nodeA.addEdge(nodeB, 4);
nodeA.addEdge(nodeC, 2);
nodeB.addEdge(nodeC, 1);
nodeB.addEdge(nodeD, 5);
nodeC.addEdge(nodeD, 8);
nodeC.addEdge(nodeE, 10);
nodeD.addEdge(nodeE, 2);
// Finding the shortest path
List shortestPath = findShortestPath(nodeA, nodeE);
// Displaying the shortest path
System.out.println("Shortest Path:");
for (Node node : shortestPath) {
System.out.print(node.name + " -> ");
}
System.out.println("END");
}
}

Download 36,9 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish