graph-theory

Algorithm for solving this distributing beads puzzle?

落花浮王杯 提交于 2019-12-04 11:16:53
问题 Lets say you have a circle (like below) with N spots, and you have N beads distributed in the slots. Here's an example: Each bead can be moved clockwise for X slots, which costs X^2 dollars. Your goal is to end up with one bead in each slot. What is the minimum amount of money you have to spend to achieve this task? More interesting variation of this problem: Algorithm for distributing beads puzzle (2)? 回答1: In this answer I assume beads can only be moved once. Otherwise it would be evident

Algorithm for Grouping

a 夏天 提交于 2019-12-04 10:58:59
I am trying to help someone write a program that I thought would be easy, but of course it never is :) I am trying to take a class roster (usually between 10-20 students) and effectivly uniquely pair off each classmate to another to make unique groups. Therefore in a class of 10 people, you can have 9 groups. It needs to be able to handle odd number of students too, adding to my confusion. I was looking at doing this in Java, but that is flexible. Any ideas on an algorithmic way to guarantee a)not infinite looping (ending with 2 people who have already been partners) and b) I am aiming for

Determine if a given weighted graph has unique MST

空扰寡人 提交于 2019-12-04 10:56:44
问题 I'm looking for an algorithm (or any other way) to determine if a given weighted graph has unique MST (Minimum spanning tree) in O(ElogV)? I don't know anything about the weights (e.g. weight(e1) != weight(e2)), and the algorithm just return True if this graph has only one unique MST or False if not. I started by using Kruskal's algo, and check if find-set(u)==find-set(v) so there is a circle in the MST, but this way does not cover all the scenarios as I thought :( Thanks a lot! Tomer. 回答1:

Looking for algorithm finding euler path

↘锁芯ラ 提交于 2019-12-04 10:31:08
问题 I'm looking for an algorithm to find an Euler path in a graph. I've seen a good one a couple of weeks ago but I can't find it now, I remember there was tagging edges, something with even/odd connections... Do you know a similar, simple and straightforward algorithm? 回答1: From Graph-Magics.com, for an undirected graph, this will give you the tour in reverse order, i.e. from the end vertex to the start vertex: Start with an empty stack and an empty circuit (eulerian path). If all vertices have

Probability to visit nodes in a random walk on graph

自闭症网瘾萝莉.ら 提交于 2019-12-04 08:41:21
I have a finite undirected graph in which a node is marked as "start" and another is marked as "goal". An agent is initially placed at the start node and it navigates through the graph randomly, i.e. at each step it chooses uniformly at random a neighbor node and moves to it. When it reaches the goal node it stops. I am looking for an algorithm that, for each node, gives an indication about the probability that the agent visits it, while traveling from start to goal. Thank you. As is often the case with graphs, it's simply a matter of knowing an appropriate way to describe the problem. One way

How can I prove the “Six Degrees of Separation” concept programmatically?

风格不统一 提交于 2019-12-04 07:51:45
问题 I have a database of 20 million users and connections between those people. How can I prove the concept of "Six degrees of separation" concept in the most efficient way in programming? link to the article about Six degrees of separation 回答1: You just want to measure the diameter of the graph. This is exactly the metric to find out the seperation between the most-distantly-connected nodes in a graph. Lots of algorithms on Google, Boost graph too. 回答2: You can probably fit the graph in memory

How to find a triangle inside a graph?

♀尐吖头ヾ 提交于 2019-12-04 07:50:05
问题 Here is an exercise in the Algorithm Design Manual. Consider the problem of determining whether a given undirected graph G = (V, E) contains a triangle or cycle of length 3. (a) Give an O(|V |^3) to find a triangle if one exists. (b) Improve your algorithm to run in time O(|V |·|E|). You may assume |V | ≤ |E|. Observe that these bounds gives you time to convert between the adjacency matrix and adjacency list representations of G. Here is my thoughts: (a) If the graph is given as an adjacency

How can I find the shortest path between 100 moving targets? (Live demo included.)

扶醉桌前 提交于 2019-12-04 07:26:11
问题 Background This picture illustrates the problem: I can control the red circle. The targets are the blue triangles. The black arrows indicate the direction that the targets will move. I want to collect all targets in the minimum number of steps. Each turn I must move 1 step either left/right/up or down. Each turn the targets will also move 1 step according to the directions shown on the board. Demo I've put up a playable demo of the problem here on Google appengine. I would be very interested

Expected number of Edges required to make a graph connected if the vertices are joined randomly?

喜欢而已 提交于 2019-12-04 04:35:11
We select two vertices randomly and connect them. So what is the expected number of edges in the graph when it becomes connected ? I tried solving it using induction but couldn't reach to an answer. What will be the right approach to this problem ? For given number of vertices n and chosen count of edges, you get the probability of the graphs connectedness as the proportion of connected graphs to all graphs. Number of all graphs is the combination number of m over n * ( n - 1). Asymptotic formula for number of connected graphs is given in The asymptotic number of labeled connected graphs with

Postgres CTE : type character varying(255)[] in non-recursive term but type character varying[] overall

杀马特。学长 韩版系。学妹 提交于 2019-12-04 03:08:34
I am new to SO and postgres so please excuse my ignorance. Attempting to get the cluster for a graph in postgres using a solution similar to the one in this post Find cluster given node in PostgreSQL the only difference is my id is a UUID and I am using varchar(255) to store this id when i try to run the query I get the following error (but not sure how to cast): ERROR: recursive query "search_graph" column 1 has type character varying(255)[] in non-recursive term but type character varying[] overall SQL state: 42804 Hint: Cast the output of the non-recursive term to the correct type.