Skip to main content
bugfix for some cyclic graphs
Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 104 bytes

⊞υθFυ«≔§ι⁰η≔⊟ιζ¿ζ«≔⊟ζδ≔EζEκ⎇⁼μ⌈δ⌊δμεF⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧«⊞υκ⊞ικ»»»F⮌υ¿⊖Lι«≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ»⊞ιE⊕§θ⁰⁼κ§ι⁰I⊟θ⊞υθFυ«≔§ι⁰η≔⊟ιζ¿ζ«≔⊟ζδ≔⁻ζ⟦δ⮌δ⟧ζF⟦⟦ηζ⟧⟦⊖ηEζEκ⎇⁼μ⌈δ⌊δμ⟧⟧«⊞υκ⊞ικ»»»F⮌υ¿⊖Lι«≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ»⊞ιE⊕§θ⁰⁼κ§ι⁰I⊟θ 

Try it online!Try it online! Link is to verbose version of code. Takes input as a pair of the number of vertices and a list of edges and outputs the polynomial as a list of coefficients in ascending order. Explanation:

⊞υθFυ« 

Start processing with the input graph.

≔§ι⁰η≔⊟ιζ 

Get the number of vertices and list of edges of this graph.

¿ζ« 

If the graph still has edges, then...

≔⊟ζδ 

Remove an edge from the list.

≔EζEκ⎇⁼μ⌈δ⌊δμε≔⁻ζ⟦δ⮌δ⟧ζ 

Calculate the result of merging the two verticesRemove any other duplicate copies of that edge togetherfrom the list.

F⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧«F⟦⟦ηζ⟧⟦⊖ηEζEκ⎇⁼μ⌈δ⌊δμ⟧⟧« 

Loop over the graph with the edge removed and the graph with the edge merged and deduplicated.

⊞υκ⊞ικ 

Push each graph both to the list of graphs to process but also to the graph that is being processed.

»»»F⮌υ 

Loop over the list of graphs in reverse order.

¿⊖Lι« 

If this graph had to be split into two simpler graphs, then...

≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ 

... push the difference between the two polynomials from the simpler graphs to this graph, ...

»⊞ιE⊕§θ⁰⁼κ§ι⁰ 

... otherwise push the polynomial for a trivial graph to this graph.

I⊟θ 

Output the polynomial for the original graph.

Charcoal, 104 bytes

⊞υθFυ«≔§ι⁰η≔⊟ιζ¿ζ«≔⊟ζδ≔EζEκ⎇⁼μ⌈δ⌊δμεF⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧«⊞υκ⊞ικ»»»F⮌υ¿⊖Lι«≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ»⊞ιE⊕§θ⁰⁼κ§ι⁰I⊟θ 

Try it online! Link is to verbose version of code. Takes input as a pair of the number of vertices and a list of edges and outputs the polynomial as a list of coefficients in ascending order. Explanation:

⊞υθFυ« 

Start processing with the input graph.

≔§ι⁰η≔⊟ιζ 

Get the number of vertices and list of edges of this graph.

¿ζ« 

If the graph still has edges, then...

≔⊟ζδ 

Remove an edge from the list.

≔EζEκ⎇⁼μ⌈δ⌊δμε 

Calculate the result of merging the two vertices of that edge together.

F⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧« 

Loop over the graph with the edge removed and the graph with the edge merged and deduplicated.

⊞υκ⊞ικ 

Push each graph both to the list of graphs to process but also to the graph that is being processed.

»»»F⮌υ 

Loop over the list of graphs in reverse order.

¿⊖Lι« 

If this graph had to be split into two simpler graphs, then...

≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ 

... push the difference between the two polynomials from the simpler graphs to this graph, ...

»⊞ιE⊕§θ⁰⁼κ§ι⁰ 

... otherwise push the polynomial for a trivial graph to this graph.

I⊟θ 

Output the polynomial for the original graph.

Charcoal, 104 bytes

⊞υθFυ«≔§ι⁰η≔⊟ιζ¿ζ«≔⊟ζδ≔⁻ζ⟦δ⮌δ⟧ζF⟦⟦ηζ⟧⟦⊖ηEζEκ⎇⁼μ⌈δ⌊δμ⟧⟧«⊞υκ⊞ικ»»»F⮌υ¿⊖Lι«≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ»⊞ιE⊕§θ⁰⁼κ§ι⁰I⊟θ 

Try it online! Link is to verbose version of code. Takes input as a pair of the number of vertices and a list of edges and outputs the polynomial as a list of coefficients in ascending order. Explanation:

⊞υθFυ« 

Start processing with the input graph.

≔§ι⁰η≔⊟ιζ 

Get the number of vertices and list of edges of this graph.

¿ζ« 

If the graph still has edges, then...

≔⊟ζδ 

Remove an edge from the list.

≔⁻ζ⟦δ⮌δ⟧ζ 

Remove any other duplicate copies of that edge from the list.

F⟦⟦ηζ⟧⟦⊖ηEζEκ⎇⁼μ⌈δ⌊δμ⟧⟧« 

Loop over the graph with the edge removed and the graph with the edge merged.

⊞υκ⊞ικ 

Push each graph both to the list of graphs to process but also to the graph that is being processed.

»»»F⮌υ 

Loop over the list of graphs in reverse order.

¿⊖Lι« 

If this graph had to be split into two simpler graphs, then...

≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ 

... push the difference between the two polynomials from the simpler graphs to this graph, ...

»⊞ιE⊕§θ⁰⁼κ§ι⁰ 

... otherwise push the polynomial for a trivial graph to this graph.

I⊟θ 

Output the polynomial for the original graph.

Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 104 bytes

⊞υθFυ«≔§ι⁰η≔⊟ιζ¿ζ«≔⊟ζδ≔EζEκ⎇⁼μ⌈δ⌊δμεF⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧«⊞υκ⊞ικ»»»F⮌υ¿⊖Lι«≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ»⊞ιE⊕§θ⁰⁼κ§ι⁰I⊟θ 

Try it online! Link is to verbose version of code. Takes input as a pair of the number of vertices and a list of edges and outputs the polynomial as a list of coefficients in ascending order. Explanation:

⊞υθFυ« 

Start processing with the input graph.

≔§ι⁰η≔⊟ιζ 

Get the number of vertices and list of edges of this graph.

¿ζ« 

If the graph still has edges, then...

≔⊟ζδ 

Remove an edge from the list.

≔EζEκ⎇⁼μ⌈δ⌊δμε 

Calculate the result of merging the two vertices of that edge together.

F⟦⟦ηζ⟧⟦⊖ηΦε⁼λ⌕εκ⟧⟧« 

Loop over the graph with the edge removed and the graph with the edge merged and deduplicated.

⊞υκ⊞ικ 

Push each graph both to the list of graphs to process but also to the graph that is being processed.

»»»F⮌υ 

Loop over the list of graphs in reverse order.

¿⊖Lι« 

If this graph had to be split into two simpler graphs, then...

≔⊟⊟ιη⊞ιE⊟⊟ι⁻κ§ηλ 

... push the difference between the two polynomials from the simpler graphs to this graph, ...

»⊞ιE⊕§θ⁰⁼κ§ι⁰ 

... otherwise push the polynomial for a trivial graph to this graph.

I⊟θ 

Output the polynomial for the original graph.