call(back)
Algorithms & Data StructuresmediumSign in to star problems

Transition Graph From Access Logs

You have an unsorted list of web-resource access logs. Each entry is [timestamp, userId, resourceId], with the timestamp in seconds since midnight; all entries are from one day, and a user's timestamps are distinct.

Order each user's accesses by time to form their path. Every path starts at a virtual START state and ends with a transition from its last resource to a virtual END state. Build the transition graph as an adjacency map: for every source state, including START and excluding END, map each possible next state to its probability, count(source -> next) / total transitions out of source.

logs = [["357", "u", "r2"], ["1262", "u", "r1"], ["1462", "u", "r2"],
        ["1060", "u", "r1"], ["756", "u", "r3"], ["1090", "u", "r3"]]

path: START -> r2 -> r3 -> r1 -> r3 -> r1 -> r2 -> END
->  { START: { r2: 1 }, r2: { r3: 0.5, END: 0.5 }, r3: { r1: 1 }, r1: { r3: 0.5, r2: 0.5 } }

Consecutive accesses to the same resource are valid transitions. Timestamps arrive as strings; compare them numerically.

Hints

Hint 1

Group by user, sort each group by numeric timestamp, and walk each path with a sliding pair (previous, current) starting from START and finishing with (last, END).

Hint 2

Count transitions in a map of maps, source -> next -> count, keeping a total per source; the second pass divides.

Hint 3

Parsing "58523" as a string would sort it before "620"; convert first.

Topics

Asked at

Similar Problems6