Emperor Daowu of Northern Wei (; 4 August 3717th day of the 7th month of the 34th year of the Jian'guo era, per Emperor Daowu's biography in Book of Wei – 6 November 409wuchen day of the 10th month of the 6th year of the Tian'ci era, per Emperor Daowu's biography in Book of Wei), personal name Tuoba Gui (), né Tuoba Shegui (), was the founding emperor of the Northern Wei dynasty of China. He was the grandson of the last prince of Dai, Tuoba Shiyiqian. After the fall of the Dai state to the Former Qin in 376, he was presumed to be the eventual successor to the Dai throne. After the Former Qin fell into disarray in 383 following its defeat by Eastern Jin forces at the Battle of Fei River, Tuoba Gui took the opportunity to reestablish Dai in 386. He soon changed the dynasty's name to Wei and declared himself a prince. He was initially a vassal of the Later Yan, but after defeating Later Yan emperor Murong Bao in 397 and seizing most of Later Yan's territory, he claimed the imperial title in 398.

Emperor Daowu was commonly regarded as a brilliant general, but cruel and arbitrary in his rule, particularly toward the end of his reign. In 409, as he considered killing his concubine Consort Helan, his son Tuoba Shao () the Prince of Qinghe, by Consort Helan, killed him, but was soon defeated by the crown prince Tuoba Si (Emperor Mingyuan) who then took the throne.

Life before founding of Northern Wei

Birth and childhood According to official accounts, Tuoba Gui was born in 371, after his father Tuoba Shi (), the son and heir apparent to Tuoba Shiyiqian the Prince of Dai, died earlier in the year from an injury he suffered while protecting Tuoba Shiyiqian from an assassination attempt by the general Baba Jin (). His mother was Tuoba Shi's wife Heiress Apparent Helan, the daughter of the powerful tribal chief and Dai vassal Helan Yegan (). Tuoba Shiyiqian, while mourning his son's death, was very pleased by the grandson's birth, and he declared a general pardon in his state and named the child Tuoba Shegui. (Other than the reference to the naming at birth, however, the name "Shegui" was scantily mentioned in historical accounts of the rest of his life, and presumably was shortened to "Gui" for simplicity.)

Around New Years 377, Former Qin launched a major attack against Dai. Tuoba Shiyiqian temporarily fled his capital Yunzhong (雲中, in modern Hohhot, Inner Mongolia), but returned after Former Qin retreated. After his return, however, his nephew Tuoba Jin () convinced his oldest surviving son Tuoba Shijun () that Tuoba Shiyiqian was considering naming one of the sons of his wife Princess Murong (a Former Yan princess) as heir and killing him. In response, Tuoba Shijun ambushed his father and brothers and killed them. This led to a collapse of Dai forces, and Former Qin forces occupied Yunzhong without a fight.

In the disturbance, Tuoba Gui's mother Lady Helan initially fled to her brother Helan Na (), who had taken over as tribal chief after Helan Yegan's death. Later, the Former Qin emperor Fu Jiān considered taking Tuoba Gui to the Former Qin capital Chang'an, but Tuoba Shiyiqian's secretary Yan Feng () convinced Fu Jiān to instead allow Tuoba Gui to remain in Dai lands to be groomed as the eventual Dai prince, arguing that this would be the best way to maintain the tribes' allegiance to Former Qin. Meanwhile, Fu Jiān divided the Dai tribes into two groups, commanded by the Xiongnu chiefs Liu Kuren () and Liu Weichen (). Tuoba Gui, along with his mother, went to live with Liu Kuren, who honored the child as a prince.

Adolescence Little is known about Tuoba Gui's life until 385, by which time Former Qin, which had come close to uniting all of China, had fallen into great disturbance in light of rebellions throughout the empire. In 384, Liu Kuren had attempted to aid Fu Jiān's son Fu Pi, who was then under siege by Murong Chui, the founder of Later Yan, but was assassinated by Muyu Chang (), the son of a Later Yan noble; he was succeeded by his brother Liu Toujuan (). In 385, however, Liu Toujuan was assassinated by Liu Kuren's son Liu Xian (), who took over as chief and viewed Tuoba Gui, now 14, as a threat. However, his subordinates Balie Liujuan () and Qiumuling Chong () found out, and at Balie's instruction Qiumuling escorted Tuoba Gui to his uncle Helan Na, who put Tuoba Gui under his protection. In 386, at the urging of previous Dai officials, Helan Na supported Tuoba Gui in reassuming the title of Prince of Dai.

Alternative version However, an alternative version of Tuoba Gui's early life was presented in documents such as the Book of Jin and Book of Song—the official histories of the rival Jin dynasty (266–420) and Liu Song dynasty. Under this version, Tuoba Gui was not Tuoba Shiyiqian's grandson, but his son—and was born significantly earlier than the official 371 date, of Tuoba Shiyiqian's wife Princess Murong. When Former Qin attacked in 377, it was Tuoba Gui who restrained his father Tuoba Shiyiqian and surrendered. Fu Jiān, offended at this act of betrayal, exiled Tuoba Gui. When Murong Chui, his uncle, declared Later Yan in 384, Tuoba Gui joined him, and was later put in charge of his father's tribes through a military campaign waged by Later Yan. Then, later, in order to avoid having the people know Tuoba Gui's status as a traitor to his father, the official version of his personal history was manufactured.

This version is not well attested but would solve a number of apparent contradictions in early Northern Wei history. These difficulties apparent in the official version include:

How Tuoba Gui's father, Tuoba Shi, had a nearly identical name to the perpetrator of the patricide, Tuoba Shijun. If both names were manufactured by Tuoba Gui's later apologists, then the similarity in name could be explained as lack of creativity. How Tuoba Han (), mentioned as Tuoba Shi's younger brother, was also referred to in some official sources as Tuoba Gui's younger brother; as Tuoba Yi (), Tuoba Han's son, played an important role early in Northern Wei history as a key diplomat and general, he would be too old to be possibly Tuoba Gui's nephew if Tuoba Gui were born in 371. Obviously, if the official history, which stated that Tuoba Yi was Tuoba Gui's cousin, were correct, then there is no problem. How Tuoba Gui appeared too willing to turn against his maternal uncles the Helans early in his reign. If he was actually the son of Princess Murong, then he would not be related to the Helans, and his campaigns against them seemed less problematic. (Of course, it would then render it problematic how he eventually attacked and seized most of Later Yan's history, as the Later Yan emperor Murong Bao would be his cousin.) How Tuoba Gui could have a younger brother—as given and undisputed in official history—Tuoba Gu () -- described as also being a son of his mother Lady Helan, if he himself were born after Tuoba Shi's death. (Two alternative explanations exist, however—it could be that Tuoba Gu was actually born of a concubine of Tuoba Shi but raised by Lady Helan, or it could be that Lady Helan remarried after Tuoba Shi's death, to another member of the Tuoba clan—possibly Tuoba Yi's father Tuoba Han—and therefore her younger son Tuoba Gu would also carry the Tuoba name. The latter possibility is attested in that another semi-contradictory version of the official history stated that Tuoba Gu was Tuoba Yi's younger brother. Another possible explanation—that Tuoba Gu was a twin younger brother—appears unlikely, as the official accounts of Tuoba Gui's birth did not suggest the possibility of twin birth.) How Tuoba Gui's oldest son Tuoba Si, born in 392, was said to be a late-arriving son. According to the official chronology, Tuoba Gui would only be 21 at this point, and it might be difficult to comprehend his being characterized as having had a late fatherhood. On the other hand, during his lifetime, particularly among non-Han peoples, marriage and childbirth often happened during adolescence. How Tuoba Gui appeared to begin to show signs of mental deterioration when he was still just in his late 30s, with signs of paranoia that appeared to be more characteristic of men of much older age. The official version attribute this to poisoning from powders given him by alchemists, which is not an unreasonable explanation, however, or it also could have been that the paranoia had nothing to do with mental deterioration.

Whether Cui Hao, the prime minister of Tuoba Gui's grandson Emperor Taiwu of Northern Wei, propagated this version, and whether that contributed to Emperor Taiwu's execution of not only himself but his entire clan, is not completely clear, but appeared likely.

As Prince of Wei

Establishment of rule For the first several years of his reign, Tuoba Gui had to endure constant gravitating of positions by tribal chiefs, and his position was not secure. As he gradually asserted his leadership, however, the tribal chiefs began to coalesce around him.

In spring 386, Tuoba Gui set his capital at Shengle (盛樂, in modern Hohhot, Inner Mongolia), and was said to encourage agriculture to try to rest his people. In summer 386, he changed his title to Prince of Wei (and thus the state became known in history as Northern Wei).

In fall 386, with support of Western Yan and Liu Xian, Tuoba Gui's youngest uncle Tuoba Kuduo () made a claim to the throne, and there were many chiefs under Tuoba Gui who secretly conspired with Tuoba Kuduo, causing Tuoba Gui to panic to flee to his maternal uncles' Helan tribe, while seeking assistance from Later Yan. Later Yan's emperor Murong Chui sent his son Murong Lin to assist, and together they defeated Tuoba Kuduo, who fled to and was executed by Liu Weichen.

Around the new year 387, Murong Chui offered the titles of Western Chanyu and Prince of Shanggu to Tuoba Gui, but as the title of Prince of Shanggu was not as honored of one as Prince of Wei, Tuoba Gui refused them.

Despite Later Yan's assistance of him and his status as a Later Yan vassal, Tuoba Gui began to secretly consider whether he could eventually conquer Later Yan. In 388, he sent his cousin Tuoba Yi the Duke of Jiuyuan to offer tributes to Murong Chui but also to observe the Later Yan court, to consider whether he would have eventual chance of attacking it. Murong Yi concluded that Murong Chui was growing old, and that his crown prince Murong Bao was incompetent—and that there were many potential claimants who would weaken Later Yan. This encouraged Tuoba Gui greatly in his eventual planning.

In 391, Helan Na's brother Helan Rangan () plotted to kill Helan Na, and the brothers engaged in wars against each other. Tuoba Gui took this opportunity to request Later Yan to jointly attack the Helan tribe—notwithstanding Helan Na and Helan Rangan's status as his uncles. In summer 391, Murong Lin captured Helan Na and Helan Rangan, but allowed Helan Na to remain free and be in command of his tribe, while taking Helan Rangan as a prisoner. It was after this campaign that Murong Lin, seeing Tuoba Gui's abilities, suggested to Murong Chui that Tuoba Gui be detained. Murong Chui refused.

In fall 391, an incident occurred that would lead to the break of relations between Later Yan and Northern Wei. That year, Tuoba Gui sent his brother Tuoba Gu to Later Yan to offer tribute, and Murong Chui's sons detained Tuoba Gu and ordered Tuoba Gui to offer horses to trade for Tuoba Gu's freedom. Tuoba Gui refused and broke off relations with Later Yan, instead entering into an alliance with Western Yan.