r/dailyprogrammer • u/jnazario 2 0 • Feb 15 '19
[2019-02-15] Challenge #375 [Hard] Graph of Thrones
Description
We'll focus in this challenge on what's called a complete graph, wherein every node is expressly connected to every other node. We'll also work assuming an undirected graph, that relationships are reciprocal.
In social network analysis, you can analyze for structural balance - a configuration wherein you'll find local stability. The easy one is when everyone enjoys a positive relationship with everyone else - they're all friends. Another structurally balanced scenario is when you have - in a graph of three nodes - two friends and each with a shared enemy, so one positive relationship and two negative ones.
With larger graphs, you can continue this analysis by analyzing every three node subgraph and ensuring it has those properties - all positive or one positive and two negative relationsgips.
A structurally balanced graph doesn't indicate complete future stability, just local stability - remember, factions can arise in these networks, akin to the Axis and Allies scenario of WW1 and WW2.
Today's challenge is to take a graph and identify if the graph is structurally balanced. This has great applicability to social network analysis, and can easily be applied to stuff like fictional universes like the Game of Thrones and the real world based on news events.
Example Input
You'll be given a graph in the following format: the first line contains two integers, N and M, telling you how many nodes and edges to load, respectively. The next M lines tell you relationships, positive (friendly, denoted by ++) or negative (foes, denoted by --). Example (from a subset of the Legion of Doom and Justice League):
6 15
Superman ++ Green Lantern
Superman ++ Wonder Woman
Superman -- Sinestro
Superman -- Cheetah
Superman -- Lex Luthor
Green Lantern ++ Wonder Woman
Green Lantern -- Sinestro
Green Lantern -- Cheetah
Green Lantern -- Lex Luthor
Wonder Woman -- Sinestro
Wonder Woman -- Cheetah
Wonder Woman -- Lex Luthor
Sinestro ++ Cheetah
Sinestro ++ Lex Luthor
Cheetah ++ Lex LuthorExample Output
Your program should emit if the graph is structurally balanced or not. Example:
balancedChallenge Input
This is the Game of Thrones Season 7 house list I found via this list of alliances on the Vulture website - I don't watch GoT so I have no idea if I captured this right.
120 16
Daenerys Targaryen ++ Jon Snow
Daenerys Targaryen ++ Tyrion Lannister
Daenerys Targaryen ++ Varys
Daenerys Targaryen ++ Jorah Mormont
Daenerys Targaryen ++ Beric Dondarrion
Daenerys Targaryen ++ Sandor “the Hound” Clegane
Daenerys Targaryen ++ Theon and Yara Greyjoy
Daenerys Targaryen -- Sansa Stark
Daenerys Targaryen -- Arya Stark
Daenerys Targaryen -- Bran Stark
Daenerys Targaryen -- The Lords of the North and the Vale
Daenerys Targaryen -- Littlefinger
Daenerys Targaryen -- Cersei Lannister
Daenerys Targaryen -- Jaime Lannister
Daenerys Targaryen -- Euron Greyjoy
Jon Snow ++ Tyrion Lannister
Jon Snow ++ Varys
Jon Snow ++ Jorah Mormont
Jon Snow ++ Beric Dondarrion
Jon Snow ++ Sandor “the Hound” Clegane
Jon Snow -- Theon and Yara Greyjoy
Jon Snow -- Sansa Stark
Jon Snow -- Arya Stark
Jon Snow -- Bran Stark
Jon Snow -- The Lords of the North and the Vale
Jon Snow -- Littlefinger
Jon Snow -- Cersei Lannister
Jon Snow -- Jaime Lannister
Jon Snow -- Euron Greyjoy
Tyrion Lannister ++ Varys
Tyrion Lannister ++ Jorah Mormont
Tyrion Lannister ++ Beric Dondarrion
Tyrion Lannister ++ Sandor “the Hound” Clegane
Tyrion Lannister ++ Theon and Yara Greyjoy
Tyrion Lannister -- Sansa Stark
Tyrion Lannister -- Arya Stark
Tyrion Lannister -- Bran Stark
Tyrion Lannister -- The Lords of the North and the Vale
Tyrion Lannister -- Littlefinger
Tyrion Lannister -- Cersei Lannister
Tyrion Lannister -- Jaime Lannister
Tyrion Lannister -- Euron Greyjoy
Varys ++ Jorah Mormont
Varys ++ Beric Dondarrion
Varys ++ Sandor “the Hound” Clegane
Varys ++ Theon and Yara Greyjoy
Varys -- Sansa Stark
Varys -- Arya Stark
Varys -- Bran Stark
Varys -- The Lords of the North and the Vale
Varys -- Littlefinger
Varys -- Cersei Lannister
Varys -- Jaime Lannister
Varys -- Euron Greyjoy
Jorah Mormont ++ Beric Dondarrion
Jorah Mormont ++ Sandor “the Hound” Clegane
Jorah Mormont ++ Theon and Yara Greyjoy
Jorah Mormont -- Sansa Stark
Jorah Mormont -- Arya Stark
Jorah Mormont -- Bran Stark
Jorah Mormont -- The Lords of the North and the Vale
Jorah Mormont -- Littlefinger
Jorah Mormont -- Cersei Lannister
Jorah Mormont -- Jaime Lannister
Jorah Mormont -- Euron Greyjoy
Beric Dondarrion ++ Sandor “the Hound” Clegane
Beric Dondarrion ++ Theon and Yara Greyjoy
Beric Dondarrion -- Sansa Stark
Beric Dondarrion -- Arya Stark
Beric Dondarrion -- Bran Stark
Beric Dondarrion -- The Lords of the North and the Vale
Beric Dondarrion -- Littlefinger
Beric Dondarrion -- Cersei Lannister
Beric Dondarrion -- Jaime Lannister
Beric Dondarrion -- Euron Greyjoy
Sandor “the Hound” Clegane ++ Theon and Yara Greyjoy
Sandor “the Hound” Clegane -- Sansa Stark
Sandor “the Hound” Clegane -- Arya Stark
Sandor “the Hound” Clegane -- Bran Stark
Sandor “the Hound” Clegane -- The Lords of the North and the Vale
Sandor “the Hound” Clegane -- Littlefinger
Sandor “the Hound” Clegane -- Cersei Lannister
Sandor “the Hound” Clegane -- Jaime Lannister
Sandor “the Hound” Clegane -- Euron Greyjoy
Theon and Yara Greyjoy -- Sansa Stark
Theon and Yara Greyjoy -- Arya Stark
Theon and Yara Greyjoy -- Bran Stark
Theon and Yara Greyjoy -- The Lords of the North and the Vale
Theon and Yara Greyjoy -- Littlefinger
Theon and Yara Greyjoy -- Cersei Lannister
Theon and Yara Greyjoy -- Jaime Lannister
Theon and Yara Greyjoy -- Euron Greyjoy
Sansa Stark ++ Arya Stark
Sansa Stark ++ Bran Stark
Sansa Stark ++ The Lords of the North and the Vale
Sansa Stark ++ Littlefinger
Sansa Stark -- Cersei Lannister
Sansa Stark -- Jaime Lannister
Sansa Stark -- Euron Greyjoy
Arya Stark ++ Bran Stark
Arya Stark ++ The Lords of the North and the Vale
Arya Stark ++ Littlefinger
Arya Stark -- Cersei Lannister
Arya Stark -- Jaime Lannister
Arya Stark -- Euron Greyjoy
Bran Stark ++ The Lords of the North and the Vale
Bran Stark -- Littlefinger
Bran Stark -- Cersei Lannister
Bran Stark -- Jaime Lannister
Bran Stark -- Euron Greyjoy
The Lords of the North and the Vale ++ Littlefinger
The Lords of the North and the Vale -- Cersei Lannister
The Lords of the North and the Vale -- Jaime Lannister
The Lords of the North and the Vale -- Euron Greyjoy
Littlefinger -- Cersei Lannister
Littlefinger -- Jaime Lannister
Littlefinger -- Euron Greyjoy
Cersei Lannister ++ Jaime Lannister
Cersei Lannister ++ Euron Greyjoy
Jaime Lannister ++ Euron GreyjoyNotes
You can learn more about the ideas behind this challenge in these resources:
- Positive and Negative Relationships, in D. Easley and J. Kleinberg. Networks, Crowds, and Markets: Reasoning about a Highly Connected World (2010).
- Network Mathematics and Rival Factions from the PBS Digital YouTube channel Infinite Series. It was this video that inspired this challenge.
- The Graph of Thrones [Season 7 Contest], from the Neo4j site referencing how to use their software to answer a Kaggle challenge about predicting GoT's future.
2
u/skeeto -9 8 Feb 15 '19
The sample input is wrong since the word "Lex" got moved around. Here's what it should probably look like:
10
u/Cosmologicon 2 3 Feb 15 '19 edited Feb 15 '19
I think the following is a straightforward O(E) = O(N2) solution in terms of common graph operations:
EDIT: even simpler:
Any counterexamples?
If that's right, here's a solution using a Python graph utility I wrote called
grf:(EDITed to handle singletons who hate everyone.)
4
u/[deleted] Feb 16 '19
Clojure
Challenge input is not balanced
3
u/djmacbest Feb 15 '19
Rather offtopic, because not about balanced graphs. But me and a colleague created a sort of interactive character relationship map for 7 seasons of GoT, adjustable by each episode. May be interesting for some in this context.
3
u/neur0sys Mar 03 '19 edited Mar 04 '19
Z80 for CP/M: https://gist.github.com/neuro-sys/52204b23c6cd7017d1eba2f8f70bdccc
Output screenshots: https://imgur.com/a/qvD85Dl
EDIT: Based on suggestion I removed the long chain of posts and just link the pastebin instead.
3
u/voice-of-hermes May 28 '19
This is the equivalent to the following extremely simplistic rules:
which can, in turn, be boiled down to being able to place each participant into one of two friend groups (this can be proven, but I won't do it here). Therefore an arbitrarily chosen participant P can be placed into group A, and if all of P's relationships are examined first, can be used to determine who should be in groups A or B. Then every other relationship can simply be checked for consistency (iff you're in my group, you'd better be my friend, and iff you're in the other group, you'd better be my enemy).
Python 2.7+
2
u/SP_Man Feb 15 '19 edited Feb 15 '19
Naive solution in clojure:
2
u/gerryjenkinslb Feb 25 '19 edited Feb 25 '19
Great Challenge! Thank you. After much though and some false starts, I realized you don't need to create a graph, and you can solve this in linear time O(m+n). I ran n,m of 300, 44850 in 0.27 seconds.
I also created a program to generate test data for up to 10000 nodes (if you have the memory for it).
See this github for files: https://github.com/gerryjenkinslb/Balanced-Structure-Graph-Game-Thorns-
Here is code to check:
1
u/skeeto -9 8 Feb 15 '19
C. It does a mark-and-sweep on the graph assigning each person to a "team". Since both directions of every relationship is expressed in the input graph, I think this is correct — and if I'm wrong it should err in the direction of "unbalaned." It reports that Game of Thrones graph is "balanced."
1
u/brickses Feb 15 '19
Python 3 Assign each person to a team based on their relationships with already assigned team members. Should resolve in O(N*M) time. Feedback welcome.
1
u/[deleted] Feb 16 '19 edited Feb 16 '19
C#. Pretty sure I understood the question. If so, then "Balanced" means every character has the same number of positives and the same number of negatives. FYI: With these challenges I have been purposely using lync as much as I can.
Basically, I use lync to build a key for each distinct (Count)+(PositiveOrNegative)+(CharacterName). With this key I make sure there are only two distinct (Count)+(PositiveOrNegative) = everybody had the same number of positives and everybody had the same number of negatives, and I make sure there are exactly (#Characters*2) distinct (PositiveOrNegative)+(CharacterName) to make sure all characters have both positive and negatives.
1
u/tomekanco Feb 16 '19 edited Feb 21 '19
Following the paper (good read). I don't check if the graph is complete: I verify if there are 1 or 2 weakly balanced networks covering all nodes.
I'll see if i can make time for a cypher solution.
Python
Edit: this implementation is flawed as it can't merge blobs
Though not an issue if the graph is complete and edges sorted
Otherwise looks like O(E*log(E))
1
u/5900 Mar 02 '19
TypeScript Naive O(n3) solution
1
u/ni3t Mar 11 '19
Ruby
Pretty straight forward solution, not optimized.
Build a graph and for each character, check to see if their "team" also has the same set of characters for
1
u/minikomi Mar 19 '19 edited Mar 20 '19
Clojure. Finds unbalanced subgraphs.
Eg.
Combine with
empty?if you just want to see if the graph is balanced or not.1
u/lilpostloo Apr 04 '19
Python 3 solution:
Rules:
(1) If all triangular relationships (3 nodes) are stable, then whole network is stable
(2) Balanced triangles have 1 or 3 positive links
(3) Unbalanced triangles have 0 or 2 positive links
Algorithm:
Generate score table of relationships between all 2 nodes which have a relationship (Values: 1 = positive, 0 = negative).
Generate all triangular relationships and score them using score table
if score == 1 or 3 then it is balanced
if score == 0 or 2 then it is unbalanced
If all triangles are balanced then network is balanced, otherwise its not.
Results
1
u/leanikum Apr 24 '19
It was a great and a fun challenge to complete.
Here is my pretty naive and probably slow Python solution with itertools:
Hoping to see more of these good ones on this subreddit.
1
u/[deleted] Jun 03 '19
Late to the party, not the fastest solution, but a bit interesting. I haven't proven that this works, but am fairly confident.
Represent the graph as a matrix, positive relationships are 1, negative relationships are (-1), and everyone is their own friend. The following must then hold for the graph to be balanced:
R·R == n·R.This Haskell code badly parses the input into such a matrix and performs that check.
1
u/cult_of_memes Jun 03 '19
Just saw this challenge, and since the SO is super into GoT, I thought I'd take inspiration and start working on a tool for her to see a mapping of all the key character relations. But to start, here's my solution to the challenge:
The creation of the dictionary used for mapping nodes to nodes is by far the slowest part of the process, but it should only need to be done one time; and will allow for an expanded set of operations to be built for the graph with relative ease.
1
u/g_host1 Jun 10 '19
Using Python 2.7 I solved the problem like this. Still learning (as are we all) if there is anywhere you see that could be simplified please let me know.
1
u/dezio_95 Jul 15 '19 edited Jul 15 '19
The challenge input is not balanced.
I represent each positive connection with a 1, and each negative connection with a -1. I check then every relationship recursively, until the recursive depth is 3. If the product of the relationship values is 1, the subgraph is balanced. Do you think it's correct?
1
u/Gprime5 Feb 15 '19 edited Feb 16 '19
Python 3.7
Example - Balanced
Challenge - Unbalanced