r/dailyprogrammer • u/nint22 1 2 • May 06 '13
[05/06/13] Challenge #124 [Easy] Edge Sorting
(Easy): Edge Sorting
In graph theory, an "edge" is defined as the connection between two vertices. For example, if we look at the sample graph on the Wikipedia article, we would define the relationship between vertex 4 and 6 as having an edge, but vertices 5 and 3 have no edge (though they are connected, through {5,4,3} or {5,2,3,} and a few others)
Your goal is to sort a given list of edges in the correct connection-order: to make your job even easier, given edges will always form one big long line (so basically a very simplified graph, like this ). Lower index integers should be on the left / top of the sorted list, while larger index integers should be on the right / bottom of the sorted list. All edges are assigned a unique letter to help keep track of ordering.
Author: nint22
Formal Inputs & Outputs
Input Description
On standard input, you will first be given an integer, which is the number of edges you will then be given. Each given edge is defined by a letter and two integers: the letter will always be unique and represents the edge. The integers are the actual edge's vertices, and thus may be duplicate (if a vertex is shared between two edges).
Output Description
Simply list the sorted edges from the left-most ordered edge to the right-most ordered edge.
Sample Inputs & Outputs
Sample Input
The following data is a simple example, with valid output following the next section:
4
A 3 4
B 4 5
C 1 2
D 2 3Sample Output
C D A BChallenge Input
This is an example of a randomized input:
6
F 2 3
B 1 2
D 6 5
C 6 7
E 5 4
A 3 4Challenge Input Solution
None required.
Note
None
5
u/IMRed 0 1 May 06 '13
C++11, missing any kind of documentation (just like at work, eh? ;)):
4
u/pandubear 0 1 May 07 '13 edited May 07 '13
Edit: Oops, looks like I didn't read the instructions carefully enough -- my code will work for the sample input/output but the challenge input!
Vimscript! I'm actually not sure if you can create a stdin/stdout-kind of thing with Vim, but this'll take the contents of the buffer as input and modify it to give output.
4
u/Coder_d00d 1 3 May 06 '13 edited May 07 '13
Objective-C in xcode using OS X/iOS using the foundation library.
Made an object to hold an edge (name, vertex, vertex). When I create and init an edge it puts the smallest vertex number first then second for easy sorting later.
I simply define a graph as an Array of Edge Objects. As I insert edges into my graph array I do so by ordering the edges so that it keeps it sorted. So at the end I just have to display the contents of the array and it will be in the sorted order I want.
edit: this is a very long/robust solution. But can handle vertexes not being in sequence from 1 to x. Also it was good Objective-C practice :/
and my Output:
4
u/Coder_d00d 1 3 May 07 '13
C
My goal was come up with the smallest program I could that still inputs the data from the user and does the best sort possible. I can always do the sort in O(n) and it really is not a sort but just a good way to store the data in order.
spoiler on my logic:
My solution in C:
4
u/amoliski May 07 '13
My first daily challenge. It's horrible. I'm sorry. But hey, it works. Tell me why I'm wrong, please.
1
u/Coder_d00d 1 3 May 07 '13
Question on test cases.
Do the vertex numbers always go in sequence from 1 to 7? Or will they from vertex to vertex?
so like given a 5 vertex line (with 4 edges) would it have to be defined as
4
D 1 2
B 2 3
C 3 4
A 4 5
(1)--D--(2)--B--(3)--C--(4)--A--(5)
or could it be defined like this as well?
4
D 10 11
B 11 15
C 15 20
A 20 30
(10)--D--(11)--B--(15)--C--(20)--A--(30)
1
u/0x746d616e May 07 '13
Solution in Go, using array indexing:
Output:
3
u/skeeto -9 8 May 06 '13
JavaScript. Lacking standard input, suppose input is formatted like so,
Edge sorting function:
Using the challenge input:
Common Lisp. Reading input looks like this.
Edge sort function (destructive):
The challenge input:
3
u/DonBiggles May 07 '13
Clojure:
Output:
3
u/tanline May 08 '13
Python ~ My first time submitting a solution!
2
u/ThereminsWake May 07 '13
Python, not a very clean solution but it works.
Output for challenge input:
2
u/The_Doculope May 07 '13
Haskell solution. It will accept edge names of any length, so long as they contain no spaces. I works by taking the edge name and its first vertex, pairing them together, and sorting the list by the vertex number. It then throws away the vertex numbers.
2
u/goakley May 07 '13
Haskell solution!
2
u/moonstne May 07 '13
Takeing the "given edges will always form one big long line" rule and running with it.
python:
output:
2
u/John_Bonforte May 07 '13 edited May 07 '13
python3:
output:
getEdgesFromFile could use some error handling and so did the other function I used to read from standard input (which I didn't post).
2
u/tchakkazulu 0 2 May 07 '13
Haskell code, bucket-sort style.
2
u/yoho139 Jun 02 '13
Certainly not the cleanest way to handle command line input, and also certainly not on time, but here it is.
In Java. This takes advantage of the fact that it starts at one and that each letter's assigned numbers are sequential.
2
u/luke1979 Aug 22 '13
Very late again, but here's my solution on C#:
1
u/deds_the_scrub May 07 '13
Written in Python
1
u/Luminar_ May 08 '13
My solution in python (second one, first one didn't work properly):
output:
1
u/ehaliewicz May 09 '13
My first solution on here, in Common Lisp
Technically, we don't need to sort, but this solution is simpler.
1
u/dante9999 May 10 '13 edited May 10 '13
Python, nothing amazing, perhaps it's slightly primitive, but works fine.
Which for challenge input gives the output:
1
u/13467 1 1 May 10 '13
J
1
u/marekkpie May 22 '13
C++:
Lua:
0
u/[deleted] May 07 '13
[deleted] — view deleted comment
0
u/[deleted] May 07 '13
[deleted] — view deleted comment
0
u/[deleted] May 06 '13
[deleted] — view deleted comment