{"type": "session", "harness": "opencode", "id": "math-0090", "name": "graph theory"} {"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}} {"type": "message", "message": {"role": "user", "content": "Explain the key concepts of graph theory in math, with concrete details and common pitfalls. Include a runnable code example with its expected output."}} {"type": "message", "message": {"role": "assistant", "content": "**Dijkstra's Algorithm:** Find shortest path from source to all vertices.\n\n```python\nimport heapq\n\ndef dijkstra(graph, src):\n dist = {v: float('inf') for v in graph}\n dist[src] = 0\n prev = {v: None for v in graph}\n pq = [(0, src)]\n\n while pq:\n d, u = heapq.heappop(pq)\n if d > dist[u]: continue\n for v, w in graph[u].items():\n if dist[u] + w < dist[v]:\n dist[v] = dist[u] + w\n prev[v] = u\n heapq.heappush(pq, (dist[v], v))\n return dist, prev\n```\n\n**Complexity:** O((V+E) log V) with binary heap.\n**Limitation:** No negative edge weights (use Bellman-Ford instead)."}}