الگوریتم بلمن-فورد(Bellman-Ford) علیرضا متانت
1.3 هزار بار بازدید -
4 سال پیش
-
در این الگوریتم به بررسی
در این الگوریتم به بررسی نحوه عملکرد الگوریتم بلمن فورد(Bellman-Ford) و مفهوم دور منفی و وزن منفی در گراف ها پرداختم و با شکل و دیباگ مرحله به مرحله کد به زبان جاوا مراحل را برای شما عزیزان دل توضیح داده ام. موفق ، پر رزق و پر روزی باشید. کد های استفاده شده :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dynamicprograming;
import java.util.*;
import java.lang.*;
import java.io.*;
/**
*
* @author Alireza_Metanat
*/
public class Graph {
class Edge {
int src, dest, weight;
Edge()
{
src = dest = weight = 0;
}
};
int V, E;
Edge edge[];
// Creates a graph with V vertices and E edges
Graph(int v, int e)
{
V = v;
E = e;
edge = new Edge[e];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
// The main function that finds shortest distances from src
// to all other vertices using Bellman-Ford algorithm. The
// function also detects negative weight cycle
void BellmanFord(Graph graph, int src)
{
int V = graph.V, E = graph.E;
int dist[] = new int[V];
int parents[]=new int[V];
// Step 1: Initialize distances from src to all other
// vertices as INFINITE
for (int i = 0; i < V; ++i)
dist[i] = Integer.MAX_VALUE;
dist[src] = 0;
for (int i = 0; i
4 سال پیش
در تاریخ 1399/10/03 منتشر شده
است.
1,309
بـار بازدید شده