State graphs help us understand how a system moves from one situation to another. They show all possible steps and choices clearly.
State graphs and transitions in Agentic AI
class State: def __init__(self, name): self.name = name self.transitions = {} # Maps action to next State def add_transition(self, action, next_state): self.transitions[action] = next_state def next_state(self, action): return self.transitions.get(action, None) # Example usage: # state1 = State('Start') # state2 = State('Middle') # state1.add_transition('go', state2) # next = state1.next_state('go') # print(next.name) # Output: Middle
Each state has a name and a list of possible transitions.
Transitions connect one state to another based on an action or event.
empty_state = State('Empty') print(empty_state.transitions) # Output: {}
single_transition_state = State('Single') single_transition_state.add_transition('move', State('Next')) print(list(single_transition_state.transitions.keys())) # Output: ['move']
start = State('Start') middle = State('Middle') end = State('End') start.add_transition('go', middle) middle.add_transition('finish', end) print(start.next_state('go').name) # Output: Middle print(middle.next_state('finish').name) # Output: End
state = State('Loop') state.add_transition('repeat', state) print(state.next_state('repeat').name) # Output: Loop
This program creates a simple state graph with three states and transitions. It shows moving through states by actions.
class State: def __init__(self, name): self.name = name self.transitions = {} def add_transition(self, action, next_state): self.transitions[action] = next_state def next_state(self, action): return self.transitions.get(action, None) # Create states start_state = State('Start') middle_state = State('Middle') end_state = State('End') # Add transitions start_state.add_transition('go', middle_state) middle_state.add_transition('finish', end_state) middle_state.add_transition('repeat', middle_state) # loop # Print initial state print(f"Current state: {start_state.name}") # Move from start to middle current_state = start_state.next_state('go') print(f"After 'go' action, state: {current_state.name}") # Repeat in middle state current_state = current_state.next_state('repeat') print(f"After 'repeat' action, state: {current_state.name}") # Finish to end state current_state = current_state.next_state('finish') print(f"After 'finish' action, state: {current_state.name}")
Time complexity to find the next state is O(1) because transitions are stored in a dictionary.
Space complexity depends on the number of states and transitions stored.
A common mistake is forgetting to add transitions, which leads to None when moving states.
Use state graphs when you want to clearly map all possible moves and decisions in a system.
State graphs show how a system moves between different situations.
Each state has transitions triggered by actions or events.
They help plan and understand AI agent behavior step-by-step.