如何在Java中遍历通过道路连接的城市网络?

huangapple go评论68阅读模式
英文:

How to traverse through a network of cities connected by roads in java?

问题

I encountered a problem where they say, a network of cities and road connecting them is given as an array of integers. The relationship between the index of the array and value at that index determines how the network looks.

例如,一个城市网络可以用一个整数数组表示,比如:

array = {9,1,4,9,0,4,8,9,0,1}

在这个例子中,array[0] = 9 表示城市0和城市9之间有一条长度为1的连接道路。

What data structure or algorithm can I use to store this information so that I can answer questions like

哪种数据结构或算法可以用来存储这个信息,以便回答类似以下的问题:

  • How many cities are connected by a road of length 1 or 2 or 3 or...
    有多少城市通过长度为1、2、3等等的道路相连?
  • What is the maximum number of cities I can cover before I encounter a city with an odd number?

在遇到城市编号为奇数的城市之前,我最多可以覆盖多少城市?

Linear thinking is not getting me anywhere.

线性思维无法帮助我解决问题。

The main condition is that no two cities have more than one path. So it's not a closed loop.

主要条件是没有两座城市之间有多条路径。因此,这不是一个封闭的环路。

I can't use binary tree logic because even if we rearrange the network as a tree, we end up with 1 parent and varying numbers of children.

我不能使用二叉树逻辑,因为即使我们将网络重新排列成树状结构,最终我们会得到一个父节点和不同数量的子节点。

Can anyone help me solve this?

有人可以帮助我解决这个问题吗?

英文:

I encountered a problem where they say, a network of cities and road connecting them is given as an array of integers. The relationship between the index of the array and value at that index determines how the network looks.

for example a network of cities can be represented by an integer array like this
array = {9,1,4,9,0,4,8,9,0,1}

in this example array[0] = 9 means there is a link between city 0 and city 9 by a connecting road of 1.

what data structure or algorithm can I use to store this information so that i can answer questions like

how many cities are connected by road of length 1 or 2 or 3 or.....
or
what is maximum number of cities I can cover before I encounter a city with odd number.

Linear thinking is not getting me anywhere.

Main Condition is no two cities have more than one path. So its not a close loop.

I can't use binary tree logic because even if we rearrange the network as a tree, we end up with 1 parent and varying number of children.

Can anyone help me solve this ?

答案1

得分: 2

Networks like this are called graphs (wikipedia link).

There are a couple different ways you can represent a graph in a computer program, the most common one is the "adjacency list" which basically tells you what are cities you can get to from a given city with just one hop. The adjacency list for the example graph in your question would be:

[[9, 4, 8], [1, 9], 4, [9], [2, 0, 5], 4, [8], [9], [6, 0], [0, 3, 7, 1]]

This means: City 0 is connected to cities 9, 4, and 8. City 1 is connected to cities 1 and 9. City 2 is connected to city 4. And so on.

This adjacency list was built with a function like this:

int[][] adjacencyList(int[] network) {
int[][] adjacency = new int[network.length][network.length];
int[] neighborCount = new int[network.length];

for (int i = 0; i < network.length; i++) {
    if (i == network[i]) {
        adjacency[i][neighborCount[i]++] = i;
    } else {
        adjacency[i][neighborCount[i]++] = network[i];
        adjacency[network[i]][neighborCount[network[i]]++] = i;
    }
}

for (int i = 0; i < network.length; i++) {
    adjacency[i] = Arrays.copyOf(adjacency[i], neighborCount[i]);
}

return adjacency;

}

how many cities are connected by road of length 1 or 2 or 3

This question is answered with graph traversal algorithms such as breadth first search and depth first search. The basic idea is, the adjacency list tells you which cities you can get to by taking one road. If you now look at where you can get from those cities, that's where you can get with 2 roads. And so on.

what is maximum number of cities I can cover before I encounter a city with odd number

You do this by first removing all odd-numbered cities, and then finding the longest path in the resulting network. Finding the longest path in a graph is a known hard problem.

英文:

Networks like this are called graphs (wikipedia link).

There are a couple different ways you can represent a graph in a computer program, the most common one is the "adjacency list" which basically tells you what are cities you can get to from a given city with just one hop. The adjacency list for the example graph in your question would be:

[[9, 4, 8], [1, 9], [4], [9], [2, 0, 5], [4], [8], [9], [6, 0], [0, 3, 7, 1]]

This means: City 0 is connected to cities 9, 4, and 8. City 1 is connected to cities 1 and 9. City 2 is connected to city 4. And so on.

This adjacency list was built with a function like this:

int[][] adjacencyList(int[] network) {
    int[][] adjacency = new int[network.length][network.length];
    int[] neighborCount = new int[network.length];

    for (int i = 0; i < network.length; i++) {
        if (i == network[i]) {
            adjacency[i][neighborCount[i]++] = i;
        } else {
            adjacency[i][neighborCount[i]++] = network[i];
            adjacency[network[i]][neighborCount[network[i]]++] = i;
        }
    }

    for (int i = 0; i < network.length; i++) {
        adjacency[i] = Arrays.copyOf(adjacency[i], neighborCount[i]);
    }

    return adjacency;
}

> how many cities are connected by road of length 1 or 2 or 3

This question is answered with graph traversal algorithms such as breadth first search and depth first search. The basic idea is, the adjacency list tells you which cities you can get to by taking one road. If you now look at where you can get from those cities, that's where you can get with 2 roads. And so on.

> what is maximum number of cities I can cover before I encounter a city with odd number

You do this by first removing all odd-numbered cities, and then finding the longest path in the resulting network. Finding the longest path in a graph is a known hard problem.

答案2

得分: 0

你需要的是一个图。

如果你有一个城市类(City class),并且这个类有一个链表(list)来存储相连的城市(初始应为空),并且这个类有一个链接方法(link(otherCity)),你可以将对应的城市添加到相应的链表中。

// 伪代码
class City
 List<City> links;

 void link(City otherCity)
   if (!links.contains(otherCity))  
     links.add(otherCity)
   otherCity.add(this)

根据你的需求,你还可以在构造函数中提供城市链接或添加一个静态的链接方法(link(cityA, city))。

这仅适用于所有道路都是双向的情况,如果你有单向关系,你应该使用两个链表(incoming/ outgoing connected cities)。

英文:

What you need here is a graph.

If you have City class, and this class has a list of linked cities (which should be empty at start), and this class has method link(otherCity) you can add the corresponding city to the respective list.

// Pseudo code
class City
 List&lt;City&gt; links;

 void link( City otherCity)
   if (! links.contains( otherCity ) )  
     links.add( otherCity )
   otherCity.add( this )

You could also provide a city link in the constructor or add a static link(cityA, city) method, depending on your needs.

This is only for the case where all roads are two-way, if you can have one-way relationships, you should two lists (incoming/outgoing connected cities)

huangapple
  • 本文由 发表于 2020年8月11日 22:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63360241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定