Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:59:11+00:00 2026-05-13T09:59:11+00:00

I am using adjacency lists to represent a directed weighted graph and based on

  • 0

I am using adjacency lists to represent a directed weighted graph and based on the example code provided by this SO question, I have created the following:

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class _Graph {
    private Map<String, LinkedHashSet<HashMap<String, Integer>>> map = new HashMap<String, LinkedHashSet<HashMap<String, Integer>>>();

    public void addEdge(String node1, String node2, int dist) {
        LinkedHashSet<HashMap<String, Integer>> adjacent = map.get(node1);
        HashMap<String, Integer> innerMap = new HashMap<String, Integer>();
        if(adjacent==null) {
            adjacent = new LinkedHashSet<HashMap<String, Integer>>();                       
            map.put(node1, adjacent);
        }
        innerMap.put(node2, dist);
        adjacent.add(innerMap);
    }

    public boolean isConnected(String node1, String node2) {
        Set<HashMap<String, Integer>> adjacent = map.get(node1);
        if(adjacent==null) {
            return false;
        }
        return adjacent.contains(node2);
    }

    public LinkedList<HashMap<String, Integer>> adjacentNodes(String node) {
        LinkedHashSet<HashMap<String, Integer>> adjacent = map.get(node);
        if(adjacent==null) {
            return new LinkedList<HashMap<String, Integer>>();
        }
        return new LinkedList<HashMap<String, Integer>>(adjacent);
    }

}

I am having trouble making the isConnected method to work properly. Am I using a wrong data structure to represent the graph here (Map<String, LinkedHashSet<HashMap<String, Integer>>>)? The hashmap will hold the name of the connected node and the distance to it:

Map<startNode, LinkedHashSet<HashMap<endNode, distanceToEndNode>>>
  1. Basically how can I check if a node
    belongs to the adjacency list of a
    given base node? I think the problem
    is reduced to iterating properly
    over the adjacent
    Set<HashMap<String, Integer>>
    structure, or is my reasoning wrong?
  2. In my second method
    adjacentNodes(String node) I am
    returning a linked list containing
    the maps (in a set structure) of the
    connected nodes and their distances. How could I efficiently iterate to see all connections of a any given node?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:59:12+00:00Added an answer on May 13, 2026 at 9:59 am

    I think LinkedHashSet is not needed here, you can represent the graph with just a Map<String, Map<String, Integer>>.

    isConnected is basically what you already have:

    public boolean isConnected(String node1, String node2) {
        Map<String, Integer> adjacent = map.get(node1);
        if(adjacent==null) {
            return false;
        }
        return adjacent.containsKey(node2);
    }
    

    adjacentNodes just needs to pull out the entries in the hash set for the source node

    public Collection<Map.Entry<String, Integer>> adjacentNodes(String node) {
        Map<String, Integer> adjacent = map.get(node);
        if(adjacent==null) {
            return new ArrayList<Map.Entry<String, Integer>>();
        }
        return adjacent.entrySet();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It depends on the design, really. Much recursion can be… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer Something like this? I think what they've done is just… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer You can't specify a HAVING clause without having specified a… May 13, 2026 at 12:22 pm

Related Questions

I'm writing a Java program that searches for and outputs cycles in a graph.
I am unable to detect the error in the following C++ program. The code
I have a C++ program that benchmarks various algorithms on input arrays of different
I am trying to use the kernlab R package to do Support Vector Machines
I have got an undirected weighted connected graph. If I select one vertex I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.