1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
iragen [17]
3 years ago
10

Implement a Breadth-First Search of the people in the network who are reachable from person id 3980. You can implement BFS with

a queue and the pseudocode is given in CLRS. Print out the distance (number of connections) from person 3980 to every person who is reachable from 3980 via edge traversals. Note that not all people in this network may be reachable via edge traversals from user id 3980, so users that are not accessible can be ignored in BFS.
Computers and Technology
1 answer:
Gnoma [55]3 years ago
8 0

Answer:

Check the explanation

Explanation:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.LinkedList;

import java.util.Scanner;

import static org.junit.Assert.assertEquals;

/**

* CS146 Assignment 3 Node class This class is used for undirected graphs

* represented as adjacency lists The areFriends() method checks if two people

* are friends by checking if an edge exists between the two

*

*/

public class NetworkAdjList {

   // Initialize array with max number of vertices taken from SNAP

   static int max_num_vertices = 88234;

   static LinkedList<Integer>[] adjacencyList = new LinkedList[max_num_vertices];

   public static void createAdjacencyList() {

       // Initialize array elements

       for (int j = 0; j < max_num_vertices; j++) {

           adjacencyList[j] = new LinkedList<Integer>();

       }

       // Get file path of the 3980.edges file

       String filePath = "C:\\Users\\shahd\\Documents\\CS146\\Assignment 3\\Question 3\\Question3\\src\\a3\\3980.edges";

       File f = new File(filePath);

       // Use Scanner to read edges from the file and put it into adjacency list

       int a;

       int b;

       try {

           Scanner fileIn = new Scanner(f);

           while (fileIn.hasNext()) {

               a = fileIn.nextInt();

               b = fileIn.nextInt();

               adjacencyList[a].add(b);

               adjacencyList[b].add(a); // We need to add the edges both ways

           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }

   }

   public static boolean areFriends(int A, int B) {

       // If the adjacency list contains (A, B) edge, then return true, else false

       if (adjacencyList[A].contains(B)) {

           return true;

       } else {

           return false;

       }

   }

  private static void bfsHelper(boolean visited[], int currentNode, int dis, int sourceNode) {

       dis++;

       if(!visited[currentNode]) {

           visited[currentNode] = true;

           

           for(int neighbor: adjacencyList[currentNode]) {

               System.out.println(neighbor + " is at a distance of " + dis + " from " + sourceNode);

               bfsHelper(visited, neighbor, dis++, sourceNode);

           }

       }

   }

   public static void BFStraversal(int start) {

       boolean visited[] = new boolean[max_num_vertices];

       bfsHelper(visited, start, 0, start);

   }

   public static void main(String[] args) {

       /**

        * These test cases assume the file 3980.edges was used

        */

       createAdjacencyList();

       System.out.println("Testing...");

       assertEquals(areFriends(4038, 4014), true);

       System.out.println("1 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(3982, 4037), true);

       System.out.println("2 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(4030, 4017), true);

       System.out.println("3 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(4030, 1), false);

       System.out.println("4 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(1, 4030), false);

       System.out.println("5 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(4003, 3980), true);

       System.out.println("6 of 7");

       System.out.println("Testing...");

       assertEquals(areFriends(3985, 4038), false);

       System.out.println("7 of 7");

       System.out.println("Success!");

   }

}

**************************************************

You might be interested in
Given three packets of size 2,500 Bytes (caution this is Bytes not bits) each are ready inside computer 1 to be transmitted onto
gizmo_the_mogwai [7]

Answer: umm sorry

Explanation:

5 0
3 years ago
What is bespoke software???​
denis-greek [22]

Answer:

custom-built to address the specific requirements of a business

Explanation:

5 0
3 years ago
Read 2 more answers
Consider the following scenario below and recommend a solution.
sammy [17]

your account has enabled profile pictures for your users, you can manage all profile pictures for your account. Profile pictures are public and automatically approved when users upload an image in their settings. Using profile pictures can make it easier to see the users in your account and managing them gives you the ability to keep the pictures appropriate.

If Gravatars are enabled for an institution in account settings, and a user has a Gravatar but chooses not to upload a profile picture, the Gravatar will display for the user's profile picture.

If a student views another student's user details in a course and reports a profile picture as inappropriate, you can review those profile pictures and approve, lock, or delete the picture. In the courses, instructors can remove profile pictures completely from a user's details page.

5 0
3 years ago
Press the ENTER key to do what?
Blababa [14]
Change the line in word, it basically returns
7 0
3 years ago
Look at the slide.
svetlana [45]

Answer:

c

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Rachel uses a word processor to write short stories. However, most of her time is spent toying with the keys, rather than in wri
    13·2 answers
  • 7. What is the school campus’s setting<br> the school is sanford
    6·2 answers
  • A provides an easy way for workers to interact with their computers
    9·1 answer
  • There are many different types of decision-making. Which of the following is NOT a type of decision-making? a. fiat rule b. sing
    15·2 answers
  • A______ is a graphical representation of numeric data.
    8·1 answer
  • Assume that strikeCounter has already been declared to be a "pointer to int". Assume further that strikeCounter has been initial
    7·1 answer
  • A personal computer (pc) or ____ is a small computer system designed to be used by one person at a time.
    12·1 answer
  • What would be the best tool to display the following information from a basketball game?
    6·1 answer
  • 1. Define the term M.A.N.
    5·2 answers
  • What was one effect of better printing methods during the Ming Dynasty? Updated trade routes A new merchant class Increased lite
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!