Hierarchical state machines in Simulink - Time & Space Complexity
We want to understand how the time it takes to run a hierarchical state machine grows as the number of states increases.
How does adding more nested states affect the total steps the machine takes?
Analyze the time complexity of the following Simulink hierarchical state machine snippet.
% Simulink Stateflow Hierarchical State Machine Example
chart = sfroot.find('-isa', 'Stateflow.Chart');
stateA = chart.find('-isa', 'Stateflow.State', 'Name', 'StateA');
subState1 = stateA.find('-isa', 'Stateflow.State', 'Name', 'SubState1');
subState2 = stateA.find('-isa', 'Stateflow.State', 'Name', 'SubState2');
% Transition from SubState1 to SubState2
transition = stateA.find('-isa', 'Stateflow.Transition');
if isempty(transition)
transition = sfroot.create('Stateflow.Transition');
transition.Source = subState1;
transition.Destination = subState2;
end
This code sets up a hierarchical state machine with a parent state and two nested substates, then creates a transition between the substates.
Look for loops or repeated checks in the state machine execution.
- Primary operation: Checking active states and transitions during each step.
- How many times: Once per state per execution cycle, including nested states.
As the number of states and nested levels grows, the machine checks more states and transitions each cycle.
| Input Size (number of states) | Approx. Operations per cycle |
|---|---|
| 10 | About 10 state checks |
| 100 | About 100 state checks |
| 1000 | About 1000 state checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of states.
Time Complexity: O(n)
This means the time to process the state machine grows linearly with the number of states.
[X] Wrong: "Adding nested states does not increase processing time much because they are inside other states."
[OK] Correct: Each nested state still needs to be checked every cycle, so more nested states add more work.
Understanding how hierarchical state machines scale helps you design efficient models and explain your reasoning clearly in discussions.
"What if we added parallel states that run simultaneously? How would that affect the time complexity?"
Practice
hierarchical state machines in Simulink Stateflow?Solution
Step 1: Understand hierarchical state machines concept
Hierarchical state machines group smaller states inside bigger parent states to manage complexity.Step 2: Identify the main benefit in Simulink Stateflow
This grouping helps model complex systems in smaller, manageable parts.Final Answer:
To organize states inside bigger states for simpler system modeling -> Option AQuick Check:
Hierarchical states simplify models = A [OK]
- Confusing hierarchical states with speeding simulation
- Thinking transitions create states automatically
- Believing hierarchical states remove all states
Solution
Step 1: Recall how hierarchical states are shown visually
Child states are drawn inside the parent state box to show containment.Step 2: Identify correct visual representation
Only placing the child state fully inside the parent state box correctly defines hierarchy.Final Answer:
Place the child state box completely inside the parent state box -> Option CQuick Check:
Child inside parent box = B [OK]
- Placing child states outside parent states
- Using transitions to define hierarchy instead of containment
- Naming child and parent states identically
P has two child states S1 and S2. If the machine starts in P and the default child state is S1, what is the active state after initialization?Solution
Step 1: Understand hierarchical state activation
When a parent state is active, its default child state is also active.Step 2: Apply to given states
Starting in parentPactivatesPand its default childS1.Final Answer:
PandS1-> Option DQuick Check:
Parent and default child active = A [OK]
- Thinking only parent or only child is active
- Confusing default child with other child
- Ignoring hierarchical activation rules
C is not activating when the parent state P is active. What is the most likely cause?Solution
Step 1: Check child state placement in hierarchy
For a child state to activate with its parent, it must be inside the parent's boundary.Step 2: Identify cause of non-activation
IfCis outsideP, it won't activate whenPis active.Final Answer:
The child stateCis not placed inside the parent stateP-> Option AQuick Check:
Child outside parent means no activation = C [OK]
- Assuming transitions affect child activation directly
- Ignoring visual containment rules
- Confusing exit transitions with activation
Operational and child states Green, Yellow, and Red. How can hierarchical states help improve this model?Solution
Step 1: Understand hierarchical grouping benefits
Grouping child states inside a parent allows shared logic like timers or transitions at the parent level.Step 2: Apply to traffic light example
ParentOperationalcan handle common behavior, while child states represent light colors.Final Answer:
By grouping the light colors insideOperational, you can manage transitions and timers centrally -> Option BQuick Check:
Hierarchy centralizes shared logic = D [OK]
- Removing parent state loses shared control
- Using separate machines complicates coordination
- Assuming hierarchy skips states automatically
Ready to prove it?
Pass the test and this pattern moves unknown
