PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How to efficiently access the information in an undirected unweighted graph?

 June 27, 2022     graph, java     No comments   

Issue

I'm working on an assignment and I have a few problem. I implement a class Graph that can represent an un-weighted and undirected graph using Adjacency Lists. My method are for now addEdges and addVertex. The social network graph was given in an attached file (each line represents two nodes connected by an edge). I can already access the graph and see who is friend with who (please see the output). I want to find out, who have the most friend and how many friends people have on the average. How can I access this informations?

public class UndirectedGraphs {
HashMap<String, LinkedList<String>> socialNetworkAdj;

public UndirectedGraphs() {
    socialNetworkAdj = new HashMap<String, LinkedList<String>>();


}
public void addVertex(String label){
    socialNetworkAdj.put(label, new LinkedList<String>());
}

public LinkedList<String> getEdges(String label) {
    return socialNetworkAdj.get(label);
}

public void addEdges(String ver1, String ver2) {
    if (!socialNetworkAdj.containsKey(ver1)) {
        addVertex(ver1);
    }

    if (!socialNetworkAdj.containsKey(ver2)) {
        addVertex(ver2);
    }
    socialNetworkAdj.get(ver1).add(ver2);
    socialNetworkAdj.get(ver2).add(ver1);
    //System.out.println(socialNetworkAdj);

}


public static void main(String[] args) throws Exception {

    File filePath = new File("C:\\Users\\F\\Desktop\\A9\\social_network.txt");
    FileReader fr = new FileReader(filePath);
    BufferedReader br = new BufferedReader(fr);

    String[] tokens = new String[2];
    //ArrayList<String> nodes = new ArrayList<>();
    UndirectedGraphs graph = new UndirectedGraphs();
    String line;
    String var1 = tokens[0];
    String var2 = tokens[1];

    while ((line = br.readLine()) != null) {
        String[] nodes = line.split("\\s+");
        if (nodes.length == 2){
            graph.addEdges(nodes[0], nodes[1] );
        }

    }

    System.out.println("\nAnna: --> " + graph.getEdges("Anna"));
    System.out.println("\nMarie: --> " + graph.getEdges("Marie"));
    System.out.println("\nJakob: --> " + graph.getEdges("Jakob"));
    System.out.println("\nHanna: --> " + graph.getEdges("Hanna"));
    System.out.println("\nFelix: --> " + graph.getEdges("Felix"));
    System.out.println("\nEmma: --> " + graph.getEdges("Emma"));
    System.out.println("\nBen: --> " + graph.getEdges("Ben"));
    System.out.println("\nUlrike: --> " + graph.getEdges("Ulrike"));
    System.out.println("\nLutz: --> " + graph.getEdges("Lutz"));
    System.out.println("\nSofia: --> " + graph.getEdges("Sofia"));
    System.out.println("\nEmilia: --> " + graph.getEdges("Emilia"));
    System.out.println("\nMia: --> " + graph.getEdges("Mia"));
    br.close();

}

}

Output

Anna: --> [Noah, Marie]

Marie: --> [Anna, Noah, Jakob, Hanna]

Jakob: --> [Marie, Felix]

Hanna: --> [Marie, Felix, Jonas]

Felix: --> [Jakob, Hanna, Jonas, Emma, Finn, Ben]

Emma: --> [Felix, Finn]

Ben: --> [Felix, Finn, Lina]

Ulrike: --> [Wolfgang]

Lutz: --> [Stephan, Wolfgang]

Sofia: --> [Emilia]

Emilia: --> [Sofia, Luis, Mia]

Mia: --> [Emilia, Lukas]

Solution

Well you can try to find the length of the LinkedList for each node, something like this -

int total = 0; //variable to store total of friends in the graph
for (String person : graph.socialNetworkAdj.keySet()) 
{           
      int counter = socialNetworkAdj.get(person).size(); //find the length
      System.out.println(person+" has "+counter+ " friends.\n");
      total+= counter; 
}
System.out.println("Average friends : "+(float)total / graph.socialNetworkAdj.size()); //considering that the socialNetworkAdj is not empty


Answered By - LiavC
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing