New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Number of shortest paths in an Undirected Weighted Graph

by admin
4 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


import java.io.*;

import java.util.*;

class GFG {

  

    

    static class Node implements Comparator<Node> {

  

        

        public int node;

  

        

        

        public int price;

  

        public Node() {}

  

        

        public Node(int node, int price)

        {

            this.node = node;

            this.price = price;

        }

  

        

        @Override

        public int evaluate(Node node1, Node node2)

        {

            if (node1.price < node2.price)

                return -1;

            if (node1.price > node2.price)

                return 1;

            return 0;

        }

    }

  

    

    

    static void addEdge(ArrayList<ArrayList<Node> > adj,

                        int x, int y, int w)

    {

        adj.get(x).add(new Node(y, w));

        adj.get(y).add(new Node(x, w));

    }

  

    

    

    static void dijkstra(ArrayList<ArrayList<Node> > adj,

                         int src, int n, int dist[],

                         int paths[])

    {

        

        

        PriorityQueue<Node> pq

            = new PriorityQueue<Node>(n + 1, new Node());

  

        

        Set<String> settled = new HashSet<String>();

  

        

        pq.add(new Node(src, 0));

  

        dist[src] = 0;

        paths[src] = 1;

  

        

        whereas (!pq.isEmpty()) {

  

            

            int u = pq.peek().node;

  

            

            

            int d = pq.peek().price;

  

            

            pq.ballot();

  

            for (int i = 0; i < adj.get(u).measurement(); i++) {

                int to = adj.get(u).get(i).node;

                int price = adj.get(u).get(i).price;

  

                

                if (settled.incorporates(to + " " + u))

                    proceed;

  

                

                

                if (dist[to] > dist[u] + price) {

  

                    

                    pq.add(new Node(to, d + price));

  

                    

                    dist[to] = dist[u] + price;

  

                    

                    paths[to] = paths[u];

                }

  

                

                else if (dist[to] == dist[u] + price) {

                    paths[to] = (paths[to] + paths[u]);

                }

  

                

                settled.add(to + " " + u);

            }

        }

    }

  

    

    

    static void

    findShortestPaths(ArrayList<ArrayList<Node> > adj,

                      int s, int n)

    {

        

        

        int[] dist = new int[n + 5];

  

        

        

        

        int[] paths = new int[n + 5];

  

        for (int i = 0; i <= n; i++)

            dist[i] = Integer.MAX_VALUE;

  

        for (int i = 0; i <= n; i++)

            paths[i] = 0;

  

        

        

        dijkstra(adj, s, n, dist, paths);

  

        System.out.print("Shortest Paths distances are : ");

        for (int i = 1; i <= n; i++) {

            System.out.print(dist[i] + " ");

        }

  

        System.out.println();

  

        System.out.print(

            "Numbers of the shortest Paths are: ");

        for (int i = 1; i <= n; i++)

            System.out.print(paths[i] + " ");

    }

  

    

    public static void principal(String[] args)

    {

        

        int N = 9;

        int M = 14;

  

        ArrayList<ArrayList<Node> > adj = new ArrayList<>();

  

        for (int i = 0; i <= N; i++) {

            adj.add(new ArrayList<Node>());

        }

  

        addEdge(adj, 1, 2, 1);

        addEdge(adj, 2, 3, 1);

        addEdge(adj, 3, 4, 2);

        addEdge(adj, 4, 5, 1);

        addEdge(adj, 5, 6, 2);

        addEdge(adj, 6, 7, 2);

        addEdge(adj, 7, 8, 1);

        addEdge(adj, 8, 1, 1);

        addEdge(adj, 2, 8, 2);

        addEdge(adj, 3, 9, 1);

        addEdge(adj, 8, 9, 2);

        addEdge(adj, 7, 9, 2);

        addEdge(adj, 3, 6, 1);

        addEdge(adj, 4, 6, 1);

  

        

        findShortestPaths(adj, 1, N);

    }

}



Source link

Tags: GraphNumberpathsshortestUndirectedWeighted
Previous Post

TroyBoi Serves Up Low Key Trap Banger “MADTING” [LISTEN]

Next Post

A History Of An Iconic Sneaker Style

Related Posts

User Guide For UnoPim PDF Generator
Softwares

User Guide For UnoPim PDF Generator

by admin
May 31, 2025
Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components
Softwares

Infragistics Ultimate 25.1 includes updates across several of its UI toolkit components

by admin
May 29, 2025
Qt bridges the language barrier gap
Softwares

Qt bridges the language barrier gap

by admin
May 28, 2025
Find the Best Rust Software Developers for Your Project
Softwares

Find the Best Rust Software Developers for Your Project

by admin
May 26, 2025
Verification framework uncovers safety lapses in open-source self-driving system
Softwares

Verification framework uncovers safety lapses in open-source self-driving system

by admin
May 23, 2025
Next Post
A History Of An Iconic Sneaker Style

A History Of An Iconic Sneaker Style

Fire Up your Entertainment Profile : entertainment

Fire Up your Entertainment Profile : entertainment

  • Trending
  • Comments
  • Latest
Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

Anant Ambani wedding: Celebs, wealthy elite attend lavish billionaire festivities – National

March 1, 2024
10 really good gadgets that cost less than $100 – TechCrunch

10 really good gadgets that cost less than $100 – TechCrunch

December 17, 2021
Best Coding Practices For Rest API Design

Django Form | Data Types and Fields

June 9, 2023
Advancement in predicting software vulnerabilities

Advancement in predicting software vulnerabilities

May 21, 2022
Most Useful Gadgets in 2021 – Nogentech.org

Most Useful Gadgets in 2021 – Nogentech.org

July 29, 2021
Deployment Diagrams Explained in Detail, With Examples

Deployment Diagrams Explained in Detail, With Examples

August 11, 2021
Every Kathryn Hahn Film Performance, Ranked

Every Kathryn Hahn Film Performance, Ranked

December 24, 2022
Credit Scoring Software Development Guide

Credit Scoring Software Development Guide

May 13, 2022
‘The Black Phone 2’ Dials In Some Ominous Teasers

‘The Black Phone 2’ Dials In Some Ominous Teasers

May 31, 2025
Nicola Peltz Beckham shares cryptic Instagram story amid ongoing rumours of a Beckham family feud

Nicola Peltz Beckham shares cryptic Instagram story amid ongoing rumours of a Beckham family feud

May 31, 2025
THE PHOENICIAN SCHEME Hilariously Quriky and One of Wes Anderson’s Best Movies — GeekTyrant

THE PHOENICIAN SCHEME Hilariously Quriky and One of Wes Anderson’s Best Movies — GeekTyrant

May 31, 2025
Mama June and Daughters Address Family Feud, Money Dispute and Raising Chickadee’s Kid

Mama June and Daughters Address Family Feud, Money Dispute and Raising Chickadee’s Kid

May 31, 2025
Insane Clown Posse Name Favorite Rock Bands, Best Nu-Metal Rapper

Insane Clown Posse Name Favorite Rock Bands, Best Nu-Metal Rapper

May 31, 2025
The Clipse’s ‘Ace Trumpets’: The 12 Best Lines

The Clipse’s ‘Ace Trumpets’: The 12 Best Lines

May 30, 2025
Google Maps falsely told drivers in Germany that roads across the country were closed

Google Maps falsely told drivers in Germany that roads across the country were closed

May 30, 2025
Indigenous Sex Worker Drama Seventeen Begins Production, Unveils Cast

Indigenous Sex Worker Drama Seventeen Begins Production, Unveils Cast

May 30, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • ‘The Black Phone 2’ Dials In Some Ominous Teasers
  • Nicola Peltz Beckham shares cryptic Instagram story amid ongoing rumours of a Beckham family feud
  • THE PHOENICIAN SCHEME Hilariously Quriky and One of Wes Anderson’s Best Movies — GeekTyrant
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

ph3333 ph casino login