Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Graphs: Traversing, Adding & Removing Data

How do you traverse a graph?

  • There are two approaches to traversing a graph:

    • A breadth-first search 

    • A depth-first search

  • A breadth-first search is a graph traversal algorithm which systematically visits all neighbours of a given vertex before moving on to their neighbours. It then repeats this layer by layer.

  • This method makes use of a queue data structure to enqueue each node as long as it is not already in a list of visited nodes. 

image-1
  1. Take the example above, the root node, Dunkirk, would be set as the current node and then added to the visited list

Dunkirk

  1. Moving from left to right, we must check if the connected node isn’t already in the visited list, if it is not, it is enqueued to the queue. 

The first version of the queue would have appeared as

Front Pointer

Back Pointer

Position

Data

 

 

5

 

 

 

4

 

 

 

3

 

2

Hartlepool

1

Moulton

0

Bolton

-1

Empty

  1. That linked vertex is then added to the visited list. Finally, it is removed from the queue as this process repeats. This means that all nodes, from left to right are enqueued to the queue in turn, before being added to the visited list, and then dequeued from the queue

  2. Output all of the visited vertices

Our final visited list would appear as

Dunkirk, Bolton, Moulton, Hartlepool, Frogmore, Teesside, Cardiff

The final version of the queue would have appeared as

Front Pointer

Back Pointer

Position

Data

5

Cardiff

4

Teesside

3

Frogmore

2

Hartlepool

1

Moulton

0

Bolton

-1

Empty

  • In A Level Computer Science, a depth-first search is a graph traversal algorithm which uses an edge-based system

  • This method makes use of a stack data structure to push each visited node onto the stack and then pop them from the stack when there are no nodes left to visit

Examiner Tips and Tricks

There is no right method to use when performing a depth-first search, it simply depends on how the algorithm is implemented. Most mark schemes commonly traverse the left-most path first, therefore that will be used in this example.

Using the example from above, Dunkirk, would be set as the current node and then added to the visited list

Dunkirk

  1. Then check if the connected node isn’t already in the visited list, if it is not, it pushed to the stack. 

The first version of the stack would appear as

Pointer

Position

Data

 

5

 

 

4

 

 

3

 

2

Bolton

1

Moulton

0

Hartlepool

-1

Empty

  1. Next, any connected node that is not on the visited list is then pushed to the stack.

  2. Then pop the stack to remove the item and set it as the current node.

  3. Output all of the visited nodes

Our final visited list would appear as

Dunkirk, Bolton, Frogmore, Moulton, Hartlepool, Teesside, Cardiff

You could visualise how the graph has been traversed below, with the left-most column first, then the middle, then the right-most column.

image-2

Adding & removing data in graphs

  • There is no single algorithm for adding or deleting a node in a graph. This is because a graph can have a number of nodes connected to any number of other nodes.

  • As a result, it is more important that there are a clear set of instructions to easily traverse the graph to find the specific node.

image-3
  •  A Binary Tree is a special type of graph and it does have an algorithm for adding and deleting items. This is covered in the content titles’ Binary Search Trees’.

Examiner Tips and Tricks

</h

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注