Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ 24.3* (Find a shortest path) Write a program that reads a connected graph fr

ID: 3597615 • Letter: C

Question

C++

24.3* (Find a shortest path) Write a program that reads a connected graph from a file. The graph is stored in a file using the same format specified in Exercise 24.1. Your program should prompt the user to enter the name of the file, then two vertices, and displays the shortest path between the two vertices. For example, for the graph in Figure 24.17a, a shortest path between 0 and 5 may be displayed as 0 1 3 5.

Here is a sample run of the program:

Sample output

Enter a file name: c:exerciseExercise24_3a.txt

Enter two vertices (integer indexes): 0 5 The number of vertices is 6 Vertex 0: (0, 1) (0, 2) Vertex 1: (1, 0) (1, 3) Vertex 2: (2, 0) (2, 3) (2, 4) Vertex 3: (3, 1) (3, 2) (3, 4) (3, 5) Vertex 4: (4, 2) (4, 3) (4, 5) Vertex 5: (5, 3) (5, 4) The path is 0 1 3 5

Note that while the problem request that you read this data from a file, you may instead hard-code the data for the graph. You should, however request the source and destination nodes from the user for each run.

Explanation / Answer

// Java program to implement solution of producer

// consumer problem.

import java.util.LinkedList;

public class Threadexample

{

    public static void main(String[] args)

                        throws InterruptedException

    {

        // Object of a class that has both produce()

        // and consume() methods

        final PC pc = new PC();

        // Create producer thread

        Thread t1 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.produce();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Create consumer thread

        Thread t2 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    pc.consume();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Start both threads

        t1.start();

        t2.start();

        // t1 finishes before t2

        t1.join();

        t2.join();

    }

    // This class has a list, producer (adds items to list

    // and consumber (removes items).

    public static class PC

    {

        // Create a list shared by producer and consumer

        // Size of list is 2.

        LinkedList<Integer> list = new LinkedList<>();

        int capacity = 2;

        // Function called by producer thread

        public void produce() throws InterruptedException

        {

            int value = 0;

            while (true)

            {

                synchronized (this)

                {

                    // producer thread waits while list

                    // is full

                    while (list.size()==capacity)

                        wait();

                    System.out.println("Producer produced-"

                                                  + value);

                    // to insert the jobs in the list

                    list.add(value++);

                    // notifies the consumer thread that

                    // now it can start consuming

                    notify();

                    // makes the working of program easier

                    // to understand

                    Thread.sleep(1000);

                }

            }

        }

        // Function called by consumer thread

        public void consume() throws InterruptedException

        {

            while (true)

            {

                synchronized (this)

                {

                    // consumer thread waits while list

                    // is empty

                    while (list.size()==0)

                        wait();

                    //to retrive the ifrst job in the list

                    int val = list.removeFirst();

                    System.out.println("Consumer consumed-"

                                                    + val);

                    // Wake up producer thread

                    notify();

                    // and sleep

                    Thread.sleep(1000);

                }

            }

        }

    }

}