• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Graph algorithm puzzle

 
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:
You have a graph containing N vertices with zero cost edges. Traveling on an edge has no cost but a vertex itself has an associated cost.
Your objective is to remove the nodes (and corresponding edges) such that it will not be possible to travel from vertex 1 to N while minimizing the cost of eliminated vertices.

The attached image shows an example. Removing nodes 2 and 3 costs 50 and is the cheapest way to break the paths from 1 to 6.  You could remove node 1 or node 6 but that will cost 100 and 200 and so these are not optimal solutions.
test.png
 
Ranch Hand
Posts: 87
  • Number of slices to send:
    Optional 'thank-you' note:
I couldn't come up with complete solution. For now I will post a hint which I think will be useful.
1. For each given vertex, you add two vertices (say 1-a, 1-b for vertex 1), and when there is an edge between 1 and 2, you add an edge between 1-a and 2-b and an edge between 2-a and 1-b with weight as cost of vertex.
2. Now to find the minimum cost, you remove edges with non-zero weight till vertex 1 and vertex 6 are disconnected. I am not sure which algorithm to use here.
You can use Krushkal's algorithm Union Find data structure. Keep adding zero weight edges and highest weight edges till 1-a n-b are of different parent groups. If there are making 1-a and n-b belong to same group, then dont add that edge(hopefully v-a v-b edge-> removing v vertex)
 
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
Difficult one, I could not think of anything better than this:

1) make a list of all paths that start at vertex 1 and end at vertex N, each path being a Set of the vertex numbers (so that each vertex only appears once)
2) for the rest of this story, ignore the vertices 1 and N
3) for each path, determine the minimum cost of all the vertices in that path; denote it as Min(i) for path number i
3) determine, for all the vertices in the paths, their frequency, and choose the one with the highest frequency (if there are more than one, take the one with lowest cost), and suppose that vertex B has cost cB and frequency fB
4) renumber all the paths with B as 1, 2, ..., fB
5) let Min be the minimum of Min(1) + Min(2) + ... + Min(fB) and cB (that would be the cost if we eliminate vertex B)
6) for the remaining paths, repeat this procedure, and let vertex C be the most frequent vertex with frequency fC and cost cC.
7) the new minimum Min of the first fB + fC paths is the minimum of Min + (minimum of (Min(1) + Min(2) + ... + Min(fC))) and cC)
8) et cetera, until all paths are handled.
9) the requested minimum cost is now min(cost of vertex 1, Min, cost of vertex N)

No idea what the O(x) is, but hopefully less than O(2 ^N).

Paul, does there exist some ingenious algo for this, that solves it in O(log N)?

Bhaskar, can you elaborate a little, and tell how far you got?

All: is my algo correct, or just bare nonsense?


 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:
While searching I found a similar problem in an assignment for students of DSA course in UG Computer Science engineering in one of the better known engineering institutes of India. They have to implement it in C...good Lord! So I am assuming there is an interesting algorithm to solve this even though I am still not able to get a handle on it.  

I am not sure if your algo is correct or not because I am still trying to understand it. But even if I did understand it, I think I will have to implement it and run some test cases to verify.
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
Unfortunately my algo is incorrect. After typing it and going to bed again. I realised the flaws in it. Then thought of another approach, but that was O(N^3). Still thinking...
 
Bhaskar Bantupalli
Ranch Hand
Posts: 87
  • Number of slices to send:
    Optional 'thank-you' note:

can you elaborate a little, and tell how far you got?


For point 1: I am splitting a vertex into two vertices and adding an artificial edge with cost of the vertex. Now the graph will have additional n vertices and additional n-edges. For each split for vertex v, I am calling them split vertices with v-a, v-b with indices i, index + n. And for given edge from v1 to v2, I will add an edge from v1-b to v2-a (and also possibly v2-a to v1-b)
So if we want to find the cost of staying at vertex 1, we can find path 1-a to 1-b(that goes via 1-a to 1-b artificial edge introduced)
So overall, it will be easier if we are talking about shortest paths, as we can find the path between 1-a to n-b.

For strikethrough point, I am referring to Krushkal's algorithm and Union-Find Datastructure
I tried with normal dfs after getting nowhere(as after adding an artificial edge in union find datastructure, whole graph is connected), that also had no progress.
 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:
Btw, I just realized that the answer in my example is incorrect. We just need to remove node 3 (cost 20) and not node 2 & 3.
test.png
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
I was unable to come up with a clever solution, even after very long thinking, so I decided to use brute force. That sounds horrible with runtimes of O(2^N), but I think I found a way to ease the pain a little.

My idea was as I described above, albeit that the part of determining the most frequent node in all the generated paths is flawed, since there is no guarantee that the reduction achieved will the smallest one.

To represent a Path in an as cheaply way as possible, I used a List as I learned it in a Scala-course. I called it MuisList, and it has a head and a MuisList as tail. The advantage is that if I have a Path, and there are three more nodes to go to, I then get three new MuisLists that all share the existing MuisList as tail. Each node will be stored only once this way.

Next step is to determine all paths from startnode to endnode. I used plain old DFS for this, and to prevent infinite paths if there are cycles in the Graph, I made sure that a node can only appear once in a path. The method is called pathsFromTo.

Having this List of paths, I first determine all the unique nodes in these paths. Then comes the brute-force approach: first I delete all paths that contain the first node. If there are no remaining paths, then removing that node makes sure that there is no path from start to finish, and I store that first node, together with the cost of it, in a resultlist. If there are still paths, then I delete from them all paths that contain the second node, et cetera, until either we run out of nodes, or there are no remaining paths, and in that case I calculate the cost of the removed nodes.

The result is a List of costs and the list of removed nodes that go with that cost. And with that you can determine whatever you like.
That second method is called getCostsOfPaths. It uses recursion and a helper-method for that.

For Pauls example I get:
****************
Paths found:
[1, 5, 4, 3, 6]
[1, 5, 4, 3, 2, 6]
[1, 4, 3, 6]
[1, 4, 3, 2, 6]
****************
NodeRemoval[cost=100, removedNodes=[1]]
NodeRemoval[cost=50, removedNodes=[2, 3]]
NodeRemoval[cost=100, removedNodes=[2, 4]]
NodeRemoval[cost=240, removedNodes=[2, 5, 6]]
NodeRemoval[cost=230, removedNodes=[2, 6]]
NodeRemoval[cost=20, removedNodes=[3]]
NodeRemoval[cost=70, removedNodes=[4]]
NodeRemoval[cost=210, removedNodes=[5, 6]]
NodeRemoval[cost=200, removedNodes=[6]]
BUILD SUCCESSFUL (total time: 0 seconds)

The normal list of subsets from the list (1, 2, 3, 4, 5, 6) would involve 2^6 subsets, and here I only needed to check 9 of them. So, although it is O(2^N), I hope in practice it will be a lot less bad. One optimization would be that if we are creating a list of removals, and the current cost of it is higher that the minimum cost found sofar, then you can skip that path. Here is the full code:

 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:
I tried to test your code for the attached graph. I am not sure if this is how you intended to take the input but based on what I understood, I updated it for the given graph as follows:




It printed the paths correctly but threw a NPE later.


test.png
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
hi Paul,

the costs-map is incorrect, it should be a Map<Node, Cost>,. This is the input that I used:

with this output:

****************
Paths found:
[1, 2, 4, 5, 6]
[1, 2, 3, 5, 6]
****************
******************
nodes in these paths:
[1, 2, 4, 5, 6, 3]
******************
NodeRemoval[cost=20, removedNodes=[1]]
NodeRemoval[cost=30, removedNodes=[2]]
NodeRemoval[cost=70, removedNodes=[4, 5]]
NodeRemoval[cost=80, removedNodes=[4, 6]]
NodeRemoval[cost=35, removedNodes=[4, 3]]
NodeRemoval[cost=40, removedNodes=[5]]
NodeRemoval[cost=50, removedNodes=[6]]


As an aside: I wondered (in the previous posts already) why in this case the also correct removals ( 3, 5), (3, 6), (3, 4) et cetera are not mentioned.  There is a good reason for, but I am not sure whether that is correct or not. For the very simple graphs so far, it works nicely, but I don't know how it will work in the case of much larger.

Anyway, it gives you a change to eliminate a complete network in the cheapest possible way!    
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
Well, there is indeed a bug, giving an NPE, for instance if you try the paths between nodes 3 and 6. This is the renewed getCostsOfPathsHelper-method:
 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:
4-5 and 4-6 are not valid solutions unless you consider 123456 also a valid solution, right?
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
No. 123456 is not a valid path. But 45 and 46 are valid (but expensive) solutions.

I now do a sort of my candidate-nodes list, such that the start- and endvertex are at the start of the list. By the look of it, that gives another reduction of paths-to-investigate.

Do you have some other examples for me to test?
 
Paul Anilprem
Enthuware Software Support
Posts: 4828
52
  • Number of slices to send:
    Optional 'thank-you' note:

Piet Souris wrote:No. 123456 is not a valid path. But 45 and 46 are valid (but expensive) solutions.


Yes, sorry, I meant all nodes in a path such as 12356 or 12456.
 
Piet Souris
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
I implemented the shortcut that I mentioned: if some nodes are being eliminated, and the cost of these nodes is larger than the minimum cost found sofar, then that attempt is skipped. Saves some more work of what is in fact an O(2^N) method. If someone knows a real clever solution, let me know.
and
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |