{"algorithm":"diff","about":["A dependency upgrade changes more of the module graph than the one line in go.mod: modules come and go, and versions move under the ones that stay. gograph.Diff compares two graphs and returns a GraphDiff with the vertices and edges added, removed and changed, and whether the graph became weighted or acyclic.","It matches vertices by label and edges by the labels of their two ends, so the two graphs can be built in any order. A vertex changes when its weight does, and an edge when its weight or its edge label does. A removed vertex takes its edges with it, and RemovedEdges lists those edges too, so the diff is complete on its own. In an undirected graph, each edge is listed once. Two graphs where one is directed and the other isn't return ErrIncompatibleGraphs.","GraphDiff.String prints one change per line, which is what a command line tool or a CI job would show, and the GraphDiff encodes as JSON. Apply makes the changes to a copy of the first graph, so Diff of that copy and the second graph is empty. WithWeightTolerance treats weights that differ by a little as equal. Diff takes O(V+E) for the vertices and edges of both graphs."],"template":{"id":"delivery-area","name":"Delivery area","description":"Eight stops in a small town. Streets are one way, and each edge is the travel time in minutes.","family":"road","directed":true,"weighted":true,"acyclic":false,"vertices":[{"id":"Depot","x":128,"y":320},{"id":"Bakery","x":352,"y":128},{"id":"Market","x":352,"y":512},{"id":"School","x":608,"y":128},{"id":"Park","x":608,"y":352},{"id":"Clinic","x":608,"y":544},{"id":"Library","x":832,"y":224},{"id":"Station","x":864,"y":480}],"edges":[{"from":"Depot","to":"Bakery","weight":4},{"from":"Depot","to":"Market","weight":2},{"from":"Market","to":"Bakery","weight":1},{"from":"Bakery","to":"School","weight":5},{"from":"Bakery","to":"Park","weight":7},{"from":"Market","to":"Park","weight":8},{"from":"Market","to":"Clinic","weight":10},{"from":"School","to":"Library","weight":3},{"from":"Park","to":"Library","weight":2},{"from":"Park","to":"Station","weight":6},{"from":"Clinic","to":"Station","weight":2},{"from":"Library","to":"Station","weight":5}],"details":{"vertices":8,"edges":12,"directed":true,"weighted":true,"acyclic":true,"weightMin":1,"weightMax":10,"negativeWeights":false,"density":0.21428571428571427,"inDegree":{"min":0,"avg":1.5,"max":3},"outDegree":{"min":0,"avg":1.5,"max":3},"selfLoops":0,"components":1}},"problem":"Roadworks slow two streets and close a third. Which streets does a driver have to plan differently?","params":{"old":"delivery-area","new":"delivery-roadworks"},"result":{"kind":"diff","lines":["- edge Park -\u003e Station weight 6","~ edge Depot -\u003e Market weight 2 -\u003e 3","~ edge Market -\u003e Park weight 8 -\u003e 12"]},"answer":"The diff has 1 edge removed and 2 edges changed. Apply on the old graph gives a graph whose Diff with the new one is empty.","legend":{"vertex":[{"id":"idle","label":"In both graphs","color":"#64748B","shape":"hollow"},{"id":"absent","label":"Only in the new graph, not added yet","color":"#475569","shape":"hollow"},{"id":"removed","label":"Removed","color":"#D55E00","shape":"dashed-square"},{"id":"added","label":"Added","color":"#009E73","shape":"filled"},{"id":"changed","label":"Weight changed","color":"#E69F00","shape":"ring"}],"edge":[{"id":"idle","label":"In both graphs","color":"#475569","shape":"line"},{"id":"absent","label":"Only in the new graph, not added yet","color":"#475569","shape":"dashed"},{"id":"removed","label":"Removed","color":"#D55E00","shape":"dashed"},{"id":"added","label":"Added","color":"#009E73","shape":"thick"},{"id":"changed","label":"Weight or label changed","color":"#E69F00","shape":"thick"}],"panels":[{"id":"diff","title":"GraphDiff.String","kind":"list"},{"id":"numbers","title":"Applied so far","kind":"numbers"}]},"steps":[{"say":"Diff matches the vertices of the two graphs by label and their edges by the labels of their ends, and finds 3 differences. The graph shows the old one. What only the new graph has is faint until a step adds it. The steps make the changes in the order Apply does: removals, then additions, then changes.","ops":[{"panel":"diff","list":[{"key":"- edge Park -> Station weight 6"},{"key":"~ edge Depot -> Market weight 2 -> 3"},{"key":"~ edge Market -> Park weight 8 -> 12"}]},{"panel":"numbers","set":{"Vertices added":"0"}},{"panel":"numbers","set":{"Vertices removed":"0"}},{"panel":"numbers","set":{"Vertices changed":"0"}},{"panel":"numbers","set":{"Edges added":"0"}},{"panel":"numbers","set":{"Edges removed":"0"}},{"panel":"numbers","set":{"Edges changed":"0"}}]},{"say":"Remove the edge from Park to Station.","ops":[{"panel":"diff","list":[{"key":"- edge Park -> Station weight 6","note":"applied"},{"key":"~ edge Depot -> Market weight 2 -> 3"},{"key":"~ edge Market -> Park weight 8 -> 12"}]},{"e":["Park","Station"],"state":"removed"},{"panel":"numbers","set":{"Vertices added":"0"}},{"panel":"numbers","set":{"Vertices removed":"0"}},{"panel":"numbers","set":{"Vertices changed":"0"}},{"panel":"numbers","set":{"Edges added":"0"}},{"panel":"numbers","set":{"Edges removed":"1"}},{"panel":"numbers","set":{"Edges changed":"0"}}]},{"say":"The edge from Depot to Market keeps its ends and changes its weight from 2 to 3.","ops":[{"panel":"diff","list":[{"key":"- edge Park -> Station weight 6","note":"applied"},{"key":"~ edge Depot -> Market weight 2 -> 3","note":"applied"},{"key":"~ edge Market -> Park weight 8 -> 12"}]},{"e":["Depot","Market"],"state":"changed"},{"panel":"numbers","set":{"Vertices added":"0"}},{"panel":"numbers","set":{"Vertices removed":"0"}},{"panel":"numbers","set":{"Vertices changed":"0"}},{"panel":"numbers","set":{"Edges added":"0"}},{"panel":"numbers","set":{"Edges removed":"1"}},{"panel":"numbers","set":{"Edges changed":"1"}}]},{"say":"The edge from Market to Park keeps its ends and changes its weight from 8 to 12.","ops":[{"panel":"diff","list":[{"key":"- edge Park -> Station weight 6","note":"applied"},{"key":"~ edge Depot -> Market weight 2 -> 3","note":"applied"},{"key":"~ edge Market -> Park weight 8 -> 12","note":"applied"}]},{"e":["Market","Park"],"state":"changed"},{"panel":"numbers","set":{"Vertices added":"0"}},{"panel":"numbers","set":{"Vertices removed":"0"}},{"panel":"numbers","set":{"Vertices changed":"0"}},{"panel":"numbers","set":{"Edges added":"0"}},{"panel":"numbers","set":{"Edges removed":"1"}},{"panel":"numbers","set":{"Edges changed":"2"}}]},{"say":"Done. The old graph with these 3 changes is the new one: Apply makes the same changes to a copy of the old graph, and Diff of that copy and the new graph is empty."}],"traceNote":"","verification":"The steps come from a step-by-step copy of Diff. Its result is checked against gograph's on every start, and Apply on the old graph gives a graph whose Diff with the new one is empty.","snippet":"d, err := gograph.Diff(before, after)\nif err != nil {\n\treturn err // gograph.ErrIncompatibleGraphs\n}\nfmt.Println(d) // + vertex, - edge, ~ edge ... weight 5 -> 7\n\nupgraded, err := d.Apply(before)\nif err != nil {\n\treturn err\n}\nagain, err := gograph.Diff(upgraded, after)\nif err != nil {\n\treturn err\n}\nfmt.Println(again.IsEmpty()) // true"}
