THE MASS DEMONSTRATIONS and violence in Egypt during the past week may look a little like the revolution that erupted two years ago — but they are utterly different. The principal protagonists in the streets are mostly not common citizens seeking an end to dictatorship but gangs of hooligans, angry and restless youth, remnants of the former regime’s security forces and a brutal and corrupt police force that answers to no authority other than itself. As Egypt’s defense minister correctly put it Tuesday, at stake is not the overthrow of a regime but the collapse of the state into anarchy.

Egypt’s Islamist government and its secular opposition, though polarized into warring camps in recent months, have a common interest in putting an end to the chaos before it consumes the country. The question is whether leaders on both sides can set aside the overreaching agendas and uncompromising tactics that have brought them to this emergency.

President Mohamed Morsi, who won a two-round democratic election last year, has considerably more legitimacy and popular support than did former ruler Hosni Mubarak. But he and his Freedom and Justice Party, backed by the Muslim Brotherhood, have helped create the crisis by adopting some of the former regime’s tactics. Mr. Morsi has smeared reasonable opponents as criminals, tried to intimidate the press and used autocratic methods to force through his agenda. The swelling unrestlast month has its roots in the mass protests Mr. Morsi provoked last year by suspending the judiciary in order to complete a new constitution.

Opposition leaders, who range from former followers of Mr. Mubarak or his nationalist predecessor, Gamal Abdel Nasser, to liberal democrats and Christians, also have much to answer for. Having lost two elections and a referendum to Islamic forces in the past year, many appear reluctant to play by democratic rules. Some have demanded political capitulation by Mr. Morsi as the price of accepting the government’s offer of dialogue; others openly seek the overthrow of the new regime.

The weakness and intransigence of both sides have empowered anarchic forces such as the police, unreformed since the fall of the Mubarak regime, hooligans and unemployed young men, who in the past week have battled one another in Cairo and cities along the Suez Canal, killing scores. Meanwhile, the army, also outside the regime’s control, deliberates over whether to restore order, seize power for itself or remain on the sidelines.

Fortunately, there were signs this week that the politicians were beginning to see the imperative of coming together. Opposition leader Mohamed ElBaradei reversed his rejection of negotiations and called for a dialogue among his secular National Salvation Front, the Morsi government, Islamist parties outside the government and the military. On Thursday the front met with the Muslim Brotherhood and agreed to oppose violence. There is much more to discuss, including possible changes to the constitution and a law governing upcoming parliamentary elections. A new, national unity government is a worthy, if long-shot, goal. But above all, Egypt’s leaders must agree on restoring order.