Data structure - directed acyclic graph describing expressions

——The content of this section is the video content notes of P65 of Bilibili Wangdao Postgraduate Entrance Examination "Data Structure".


Table of contents

1. Definition

2. Problem description

3. Problem-solving steps

4. Special examples


1. Definition

        Directed Acyclic Graph: If there is no cycle in a directed graph, it is called a directed acyclic graph, referred to as DAG graph (Directed Acyclic Graph), as shown below shown;


2. Problem description

1. I have previously learned to use the form of a tree to represent an expression, such as the following tree:

2. It is not difficult to find that many parts of the tree above can be merged. After the merge, it becomes:

 

 3. Generally, an arithmetic expression will be given. Let us find the minimum number of vertices required. The solution steps are given below;


3. Problem-solving steps

1. For example, the question is given:

 

2. Step one: Arrange each operand in a row without repetition, as shown below:

 

3. Step 2: Mark the order in which each operator takes effect (it doesn’t matter if the order is slightly different), as shown below: 

4. Step 3: Add operators in order, paying attention to "layering", as shown below:

5. Step 4: Check whether operators at the same level can be combined layer by layer from bottom to top, as shown below:


4. Special examples

 

Guess you like

Origin blog.csdn.net/weixin_64084604/article/details/128405522