Number of shortest paths in an Undirected Weighted Graph – New Self New Life
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

Minor update (2) for Vivaldi Desktop Browser 7.5
Softwares

Minor update (2) for Vivaldi Desktop Browser 7.5

by admin
July 9, 2025
20+ Best Free Food Icon Sets for Designers — Speckyboy
Softwares

20+ Best Free Food Icon Sets for Designers — Speckyboy

by admin
July 8, 2025
Luna v1.0 & FlexQAOA bring constraint-aware quantum optimization to real-world problems
Softwares

Luna v1.0 & FlexQAOA bring constraint-aware quantum optimization to real-world problems

by admin
July 7, 2025
User Guide for Odoo MultiChannel Email Marketing
Softwares

User Guide for Odoo MultiChannel Email Marketing

by admin
July 6, 2025
Avoid these common platform engineering mistakes
Softwares

Avoid these common platform engineering mistakes

by admin
July 4, 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
Kanye West entry visa revoked by Australia after ‘Heil Hitler’ song release – National

Kanye West entry visa revoked by Australia after ‘Heil Hitler’ song release – National

July 3, 2025
‘America is back’: Foo Fighters show hits 100% capacity at Madison Square Garden – National

‘America is back’: Foo Fighters show hits 100% capacity at Madison Square Garden – National

June 22, 2021
Jane Withers, child actor and commercial star, dies at 95 – National

Jane Withers, child actor and commercial star, dies at 95 – National

August 10, 2021
Every Van Halen Album, Ranked 

Every Van Halen Album, Ranked 

August 12, 2024
How to integrate Odoo with ReactJs frontend

How to integrate Odoo with ReactJs frontend

October 7, 2024
All the glamorous celebrity looks from the Jeff Bezos, Lauren Sánchez wedding – National

All the glamorous celebrity looks from the Jeff Bezos, Lauren Sánchez wedding – National

June 28, 2025
Bones: All Of Brennan’s Interns, Ranked

Bones: All Of Brennan’s Interns, Ranked

June 15, 2021
Pinterest Launches New Video Ad Campaign to Highlight its Value in Exploring Niches

Pinterest Launches New Video Ad Campaign to Highlight its Value in Exploring Niches

September 25, 2023
Jelly Roll, Lainey Wilson, Luke Combs Among ACM Special Awards Honorees

Jelly Roll, Lainey Wilson, Luke Combs Among ACM Special Awards Honorees

July 9, 2025
YSerendept Fiber Optic USB C Cable review

YSerendept Fiber Optic USB C Cable review

July 9, 2025
Jonathan Torrens ‘Pretty Blind’ Renewed For Season 2 At AMI-tv

Jonathan Torrens ‘Pretty Blind’ Renewed For Season 2 At AMI-tv

July 9, 2025
Minor update (2) for Vivaldi Desktop Browser 7.5

Minor update (2) for Vivaldi Desktop Browser 7.5

July 9, 2025
Kesha Cancels Dallas Concert Amid Deadly Texas Floods

Kesha Cancels Dallas Concert Amid Deadly Texas Floods

July 9, 2025
Reducing Your Carbon Footprint: Personal vs. Professional Approaches

Reducing Your Carbon Footprint: Personal vs. Professional Approaches

July 9, 2025
James Gunn Says Bollywood Was a Big Inspiration for SUPERMAN — GeekTyrant

James Gunn Says Bollywood Was a Big Inspiration for SUPERMAN — GeekTyrant

July 9, 2025
Digital Marketing Statistics of 2025: H1 by the Numbers

Digital Marketing Statistics of 2025: H1 by the Numbers

July 9, 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

  • Jelly Roll, Lainey Wilson, Luke Combs Among ACM Special Awards Honorees
  • YSerendept Fiber Optic USB C Cable review
  • Jonathan Torrens ‘Pretty Blind’ Renewed For Season 2 At AMI-tv
  • 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.

New Self New Life