Advertisement
martinkotevski

APS / Lab 9 / Problem 3 - CountFacebookFriends

Dec 15th, 2016
1,996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  * Created by martin on 12/15/16.
  6.  * Dadena e edna socijalna mreza (Faceboook) vo koja za korisnicite se cuvaat
  7.  * podatoci za reden broj (celobrojna vrednost), ime i prezime. Isto taka,
  8.  * za sekoj korisnik se poznati negovite prijateli vo socijalnata mreza.
  9.  * Da se napise algoritam koj za dadeni redni broevi na dvajca korisnici go
  10.  * odreduva stepenot na razdelenost megju korisnicite t.e preku kolku
  11.  * najmalku korisnici se povrzani. (preku kolku najmalku korisnici moze da
  12.  * se stigne od edniot do drugiot korisnik) vo socijalnata mreza.
  13.  * Socijalnata mreza e pretstavena kako netezinski graf so lista na sosedstvo.
  14.  * Vo prviot red e daden brojot na korisnicite. Potoa, vo sledniot red e daden
  15.  * brojot na prijatelite na prviot korisnil (so reden broj 0), i vo slednite
  16.  * redovi se navedeni negovite prijateli so reden broj, ime i prezime.
  17.  * Ponatamy se dadeni na istion nacin informaciite za site korisnici.
  18.  * Na kraj vo poslednite dva reda se dadeni rednite broevi na dvata korisnici
  19.  * za koi treba da se odredi stepenot na razdelenost.
  20.  */
  21. public class CountFacebookFriends {
  22.  
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.         int N = Integer.parseInt(scanner.nextLine());
  26.         Graph<String> facebookGraph = new Graph<>(N);
  27.  
  28.         for (int i = 0; i < N; i++) {
  29.             int numFriends = Integer.parseInt(scanner.nextLine());
  30.             for (int j = 0; j < numFriends; j++) {
  31.                 String[] parts = scanner.nextLine().split("\\s+");
  32.                 int index = Integer.parseInt(parts[0]);
  33.                 String username = parts[1] + parts[2];
  34.                 facebookGraph.setInfo(index, username);
  35.                 facebookGraph.addEdge(i, index);
  36.             }
  37.         }
  38.  
  39.         int start = scanner.nextInt();
  40.         int end = scanner.nextInt();
  41.  
  42.         System.out.println(facebookGraph.distance(start, end));
  43.     }
  44. }
  45.  
  46. class Graph<E> {
  47.  
  48.     private int V; // number of nodes
  49.     private Node<E>[] adjList;
  50.  
  51.     public Graph(int V, E[] list) {
  52.         this.V = V;
  53.         adjList = new Node[V];
  54.         for (int i = 0; i < V; i++)
  55.             adjList[i] = new Node<>(i, list[i]);
  56.     }
  57.  
  58.     public Graph(int V) {
  59.         this.V = V;
  60.         adjList = new Node[V];
  61.     }
  62.  
  63.     public boolean areAdjacent(int x, int y) {
  64.         return adjList[x].containsNeighbour(adjList[y]);
  65.     }
  66.  
  67.     public void addEdge(int x, int y) {
  68.         if (adjList[x] == null)
  69.             adjList[x] = new Node<>(x, null);
  70.         if (adjList[y] == null)
  71.             adjList[y] = new Node<>(y, null);
  72.         adjList[x].addNeighbour(adjList[y]);
  73.     }
  74.  
  75.     public boolean deleteEdge(int x, int y) {
  76.         return adjList[x].removeNeighbour(adjList[y]);
  77.     }
  78.  
  79.     public void setInfo(int index, E info) {
  80.         if (adjList[index] == null)
  81.             adjList[index] = new Node<E>(index, info);
  82.         else
  83.             adjList[index].info = info;
  84.     }
  85.  
  86.     // slightly modified bfs algorithm
  87.     /**
  88.      * Finds the shortest distance between tho nodes in an unweighted graph
  89.      * @param start
  90.      * @param end
  91.      * @return distance from node with index 'start' to node with index 'end',
  92.      * or -1 if node with index 'end' does not exist in the graph
  93.      */
  94.     public int distance(int start, int end) {
  95.         QueueList<Node<E>> queue = new QueueList<>();
  96.         boolean[] visited = new boolean[V];
  97.         // stores distance from start node to every node
  98.         int[] distance = new int[V];
  99.  
  100.         if (adjList[start] == null)
  101.             return 0;
  102.  
  103.         queue.enqueue(adjList[start]);
  104.         visited[start] = true;
  105.         distance[end] = 0;
  106.  
  107.         while (!queue.isEmpty()) {
  108.             Node<E> node = queue.dequeue();
  109.  
  110.             for (Node<E> neighbour : node.neighbours) {
  111.                 if (!visited[neighbour.index]) {
  112.                     visited[neighbour.index] = true;
  113.                     distance[neighbour.index] = distance[node.index] + 1;
  114.                     queue.enqueue(neighbour);
  115.                 }
  116.                 if (neighbour.index == end)
  117.                     return distance[end];
  118.             }
  119.         }
  120.         return -1; // adjList[end] doesn't exist in the graph
  121.     }
  122.  
  123.     public String toString() {
  124.         StringBuilder builder = new StringBuilder();
  125.         for (Node<E> n : adjList)
  126.             builder.append(n);
  127.         return builder.toString();
  128.     }
  129.  
  130.     private class Node<T> {
  131.         int index;
  132.         T info;
  133.         LinkedList<Node<T>> neighbours;
  134.  
  135.         Node(int index, T data) {
  136.             this.index = index;
  137.             this.info = data;
  138.             neighbours = new LinkedList<Node<T>>();
  139.         }
  140.  
  141.         void addNeighbour(Node<T> node) {
  142.             neighbours.add(node);
  143.         }
  144.  
  145.         boolean containsNeighbour(Node<T> node) {
  146.             return neighbours.contains(node);
  147.         }
  148.  
  149.         boolean removeNeighbour(Node<T> node) {
  150.             if (neighbours.contains(node)) {
  151.                 neighbours.remove(node);
  152.                 return true;
  153.             }
  154.             return false;
  155.         }
  156.  
  157.         public boolean equals(Object object) {
  158.             if (object == null)
  159.                 return false;
  160.             if (this == object)
  161.                 return true;
  162.             if (this.getClass() != object.getClass())
  163.                 return false;
  164.             @SuppressWarnings("unchecked")
  165.             Node<T> other = (Node<T>) object;
  166.             return index == other.index&&info.equals(other.info);
  167.         }
  168.  
  169.         @Override
  170.         public String toString() {
  171.             StringBuilder builder = new StringBuilder();
  172.             builder.append(index);
  173.             builder.append(" ");
  174.             builder.append(info.toString());
  175.             builder.append(": ");
  176.             for (Node<T> n : neighbours) {
  177.                 builder.append(n.info.toString());
  178.                 builder.append(" -> ");
  179.             }
  180.             if (neighbours.size() > 0)
  181.                 builder.delete(builder.length() - 4, builder.length());
  182.             builder.append("\n");
  183.             return builder.toString();
  184.         }
  185.  
  186.     }
  187. }
  188.  
  189. class QueueList<E> {
  190.     private LinkedList<E> list;
  191.  
  192.     public QueueList() {
  193.         list = new LinkedList<>();
  194.     }
  195.  
  196.     public boolean isEmpty() {
  197.         return list.isEmpty();
  198.     }
  199.  
  200.     public void enqueue(E el) {
  201.         list.addLast(el);
  202.     }
  203.  
  204.     public E dequeue() {
  205.         return list.removeFirst();
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement